name: CI

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

jobs:
  backend:
    name: Backend Tests
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend

    steps:
      - uses: actions/checkout@v4

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

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[dev]"

      - name: Run tests with coverage
        run: pytest --cov=app --cov-report=term-missing --cov-fail-under=80

      - name: Upload coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: backend/htmlcov/
          retention-days: 7

  ruff:
    name: Lint
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend

    steps:
      - uses: actions/checkout@v4

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

      - name: Install dependencies
        run: pip install ruff

      - name: Run ruff
        run: ruff check .

  mypy:
    name: Type Check
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend

    steps:
      - uses: actions/checkout@v4

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

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

      - name: Run mypy
        run: mypy app

  import-linter:
    name: Import Boundary
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend

    steps:
      - uses: actions/checkout@v4

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

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

      - name: Run import-linter
        run: linter

  openapi-breaking-changes:
    name: OpenAPI Breaking Changes
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend
    # Only run on PRs — main branch push is covered by the baseline-commit step.
    if: github.event_name == 'pull_request'

    steps:
      - uses: actions/checkout@v4

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

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

      - name: Generate current OpenAPI spec
        run: python scripts/generate_openapi.py current-openapi.json

      - name: Fetch baseline spec from main
        run: |
          git fetch origin main:main
          git show main:backend/openapi.json > baseline-openapi.json 2>/dev/null || \
            echo "{}" > baseline-openapi.json

      - name: Install openapi-diff
        run: npm install -g openapi-diff

      - name: Check for breaking changes
        run: |
          set +e
          openapi-diff baseline-openapi.json current-openapi.json --format stylish 2>&1
          EXIT_CODE=$?
          if [ $EXIT_CODE -ne 0 ]; then
            echo "BREAKING CHANGE DETECTED — see output above"
            exit 1
          fi
          echo "No breaking changes found."

  openapi-baseline-commit:
    name: OpenAPI Baseline Commit
    runs-on: ubuntu-latest
    # Only run on push to main (not PRs).
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'

    steps:
      - uses: actions/checkout@v4

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

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

      - name: Generate and commit OpenAPI baseline
        run: |
          python scripts/generate_openapi.py backend/openapi.json

          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"

          git add backend/openapi.json
          git diff --cached --quiet && echo "No changes to openapi.json" || \
            git commit -m "chore: update OpenAPI baseline spec [skip ci]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>"