name: Proxym CI

on:
  push:
    branches: [main, develop]
  pull_request:
  schedule:
    - cron: '0 2 * * *'   # nightly at 2:00 AM

jobs:
  # ─────────────────────────────────────────────
  # LEVEL 1: Fast unit tests (on every push)
  # Time: < 2 minutes
  # ─────────────────────────────────────────────
  unit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Install dependencies
        run: |
          pip install -e ".[dev]"
          pip install pytest pytest-asyncio pytest-cov pytest-timeout httpx

      - name: Run unit tests
        run: |
          pytest tests/ \
            --ignore=tests/test_e2e.py \
            --ignore=tests/test_dashboard_live.py \
            --ignore=tests/test_dashboard_performance.py \
            --ignore=tests/gui/ \
            -x -q \
            --tb=short \
            --timeout=60

      - name: Code quality checks
        run: |
          ruff check src/ tests/ --quiet || true
          mypy src/proxym --ignore-missing-imports --quiet || true

  # ─────────────────────────────────────────────
  # LEVEL 2: Integration tests with Redis (on PR)
  # Time: < 10 minutes
  # ─────────────────────────────────────────────
  integration:
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' || github.event_name == 'push'
    services:
      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 5s
          --health-timeout 3s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Install dependencies
        run: |
          pip install -e ".[dev]"
          pip install pytest pytest-asyncio pytest-cov httpx

      - name: Run integration tests
        run: |
          pytest tests/test_dashboard_integration.py \
                 tests/test_api_completions.py \
                 tests/test_api_system.py \
                 tests/test_api_context.py \
                 tests/test_cli.py \
                 -v --tb=short
        env:
          REDIS_URL: redis://localhost:6379/0
          AI_PROXY_MASTER_KEY: test-key-ci
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY_TEST || 'sk-ant-test' }}
          OPENAI_API_KEY: sk-test
          OPENROUTER_API_KEY: sk-or-test

  # ─────────────────────────────────────────────
  # LEVEL 3: E2E with GUI tests (nightly)
  # Time: < 30 minutes
  # ─────────────────────────────────────────────
  e2e:
    runs-on: ubuntu-latest
    if: github.event_name == 'schedule' || contains(github.event.head_commit.message, '[e2e]')

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libvirt-daemon-system libvirt-clients

      - name: Install Python dependencies
        run: |
          pip install -e ".[dev,vm]"
          pip install pytest pytest-asyncio pytest-timeout playwright
          playwright install chromium

      - name: Run E2E tests
        run: |
          pytest tests/test_e2e.py \
                 tests/test_dashboard_live.py \
                 tests/gui/ \
                 -v --tb=long \
                 --timeout=300
        env:
          PROXYM_TEST_KVM: "1"
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY_E2E || 'sk-ant-test' }}

  # ─────────────────────────────────────────────
  # Coverage reporting
  # ─────────────────────────────────────────────
  coverage:
    runs-on: ubuntu-latest
    needs: [unit, integration]
    if: github.event_name == 'pull_request'

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Install dependencies
        run: pip install -e ".[dev]"

      - name: Generate coverage report
        run: |
          pytest tests/ \
            --ignore=tests/test_e2e.py \
            --ignore=tests/test_dashboard_live.py \
            --cov=src/proxym \
            --cov-report=xml \
            --cov-report=term-missing \
            --cov-fail-under=70

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.xml
          fail_ci_if_error: false
