name: CI

on:
  push:
    branches: [main, "claude/**", "feat/**", "fix/**"]
  pull_request:
    branches: [main]
  schedule:
    - cron: "0 2 * * 1"

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  lint:
    name: Lint & type-check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
      - run: pip install -e ".[dev,mcp]"
      - run: ruff check civic_stack/ tests/
      - run: ruff format --check civic_stack/ tests/
      - run: mypy civic_stack/shared/

  test:
    name: Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
      - run: pip install -e ".[dev]"
      - name: Run tests (VCR replay, skip browser modules)
        run: |
          python -m pytest tests/ -v --tb=short \
            --ignore=tests/ahu \
            --ignore=tests/bpjph \
            --ignore=tests/oss-nib
        env:
          VCR_RECORD_MODE: none

  test-browser:
    name: Tests (browser modules)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
      - run: pip install -e ".[dev,browser]"
      - run: playwright install chromium --with-deps
      - name: Run browser module tests
        run: |
          python -m pytest tests/ahu tests/bpjph tests/oss-nib -v --tb=short
        env:
          VCR_RECORD_MODE: none
        continue-on-error: true  # Browser tests may fail due to geo-blocking

  openapi:
    name: Export OpenAPI spec
    runs-on: ubuntu-latest
    needs: [lint, test]
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
      - run: pip install -e ".[dev,api]"
      - run: python scripts/export_openapi.py
      - uses: actions/upload-artifact@v4
        with:
          name: openapi-spec
          path: docs/openapi.json

  live-portal-check:
    name: Live portal regression check
    runs-on: ubuntu-latest
    if: github.event_name == 'schedule'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
      - run: pip install -e ".[dev]"
      - name: Run live integration tests
        run: python scripts/test_module.py --live
        env:
          PROXY_URL: ${{ secrets.PROXY_URL }}
        continue-on-error: true
