name: ci

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    name: pytest + coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install uv
        uses: astral-sh/setup-uv@v4
        with:
          version: "latest"
          enable-cache: true
      - name: Sync deps
        run: uv sync --all-groups
      - name: Run tests
        run: uv run pytest -q

  lint:
    name: ruff
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install uv
        uses: astral-sh/setup-uv@v4
        with:
          enable-cache: true
      - name: Sync deps
        run: uv sync --all-groups
      - name: ruff lint
        run: uv run ruff check src test scripts
      - name: ruff format check
        run: uv run ruff format --check src test scripts

  docs:
    name: mkdocs build + deploy to Cloudflare Pages
    runs-on: ubuntu-latest
    needs: [test, lint]
    steps:
      - uses: actions/checkout@v4
      - name: Install uv
        uses: astral-sh/setup-uv@v4
        with:
          enable-cache: true
      - name: Sync deps
        run: uv sync --all-groups

      # —— 跑测试产出 coverage.xml + junit.xml（CI 不依赖 artifact 跨 job）——
      - name: Run tests for coverage + junit artifacts
        run: uv run pytest -q

      # —— 生成徽章 ——
      - name: Generate badges (tests + coverage + ci + docs)
        run: |
          mkdir -p docs/assets/badges
          uv run genbadge coverage -i coverage.xml -o docs/assets/badges/coverage.svg
          uv run genbadge tests    -i junit.xml    -o docs/assets/badges/tests.svg
          curl -sL "https://img.shields.io/badge/CI-passing-brightgreen" > docs/assets/badges/ci.svg
          curl -sL "https://img.shields.io/badge/docs-live-blue"         > docs/assets/badges/docs.svg

      # —— 文档构建 + 部署 ——
      - name: Build docs (strict)
        run: uv run mkdocs build --strict
      - name: Deploy to Cloudflare Pages
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          # CF Pages 要求 commit message 是 UTF-8 ASCII；固定 ASCII 避免中文 commit 挂 CI
          command: pages deploy ./site --project-name=agentaily-xiangqin --branch=main --commit-message="CI deploy from GitHub Actions"
