name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.11", "3.12", "3.13"]

    steps:
      - uses: actions/checkout@v4

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

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

      - name: Install dependencies
        run: uv sync --extra dev --python ${{ matrix.python-version }}

      - name: Lint
        run: uv run ruff check claw_forge/ tests/

      - name: Type check
        run: uv run mypy claw_forge/ --ignore-missing-imports

      - name: Test
        run: uv run pytest tests/ -v --cov=claw_forge --cov-report=xml --cov-report=term-missing

      - name: Upload coverage
        if: matrix.python-version == '3.12'
        uses: codecov/codecov-action@v4
        with:
          file: coverage.xml

  screenshots:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    needs: []
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - name: Install UI deps
        run: npm --prefix ui install
      - name: Install Playwright
        run: npx --prefix ui playwright install chromium --with-deps
      - name: Capture screenshots
        run: |
          cd ui
          node scripts/mock-server.js &
          sleep 2
          npm run dev &
          sleep 8
          node scripts/screenshots.mjs
      - name: Commit screenshots
        run: |
          git config user.name "Alex Chen"
          git config user.email "alex.chen31337@gmail.com"
          git add website/assets/screenshots/
          git diff --staged --quiet || git commit -m "chore: update UI screenshots [skip ci]"
          git push
