name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    name: Test Python ${{ matrix.python-version }} (unit)
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.11", "3.12"]

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install Tesseract OCR
        run: sudo apt-get update && sudo apt-get install -y tesseract-ocr

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Install dependencies
        run: uv sync --all-extras

      - name: Run unit tests (exclude E2E)
        run: uv run python -m pytest -v -m "not e2e"

  e2e-fast:
    name: Playwright E2E smoke tests
    runs-on: ubuntu-latest
    needs: test

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install Tesseract OCR
        run: sudo apt-get update && sudo apt-get install -y tesseract-ocr

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Install dependencies
        run: uv sync --all-extras

      - name: Install Playwright browsers
        run: uv run playwright install --with-deps

      - name: Start WineBox server (background)
        run: |
          uv run python -m invoke start-background &
          for i in {1..20}; do
            if curl -sf http://localhost:8000/health > /dev/null; then
              echo "Server is up"
              break
            fi
            echo "Waiting for server..."
            sleep 3
          done

      - name: Run fast E2E suite
        env:
          WINEBOX_TEST_URL: "http://localhost:8000"
          WINEBOX_AUTH_EMAIL_VERIFICATION_REQUIRED: "false"
        run: uv run python -m invoke test-e2e-fast --verbose

  lint:
    name: Lint
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Install dependencies
        run: uv sync --all-extras

      - name: Check import sorting with isort
        run: uv run pip install isort && uv run isort --check-only --diff winebox tests
        continue-on-error: true

  build:
    name: Build package
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install build dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build

      - name: Build package
        run: python -m build

      - name: Check package
        run: |
          pip install twine
          twine check dist/*
