name: PR Check

on:
  pull_request:
    branches: [main, develop]
    types: [opened, synchronize, reopened]

jobs:
  pr-lint-test:
    name: Lint & Test (PR)
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: geo_test
        ports: ['5432:5432']
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      redis:
        image: redis:7
        ports: ['6379:6379']
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      # ── 后端检查 ──────────────────────────────────
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install backend dependencies
        run: |
          cd backend
          pip install -r requirements.txt
          pip install ruff pytest-cov

      - name: Backend lint
        run: cd backend && ruff check app/

      - name: Backend tests
        env:
          DATABASE_URL: postgresql+asyncpg://test:test@localhost:5432/geo_test
          REDIS_URL: redis://localhost:6379/0
          JWT_SECRET: test-secret-key-minimum-32-characters-long
          ENVIRONMENT: test
        run: |
          cd backend
          pytest tests/ -v --tb=short --cov=app --cov-report=xml

      # ── 前端检查 ──────────────────────────────────
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: frontend/package-lock.json

      - name: Install frontend dependencies
        run: cd frontend && npm ci

      - name: Frontend lint
        run: cd frontend && npm run lint

      - name: Frontend type check
        run: cd frontend && npx tsc --noEmit

      - name: Frontend unit tests
        run: cd frontend && npm run test:ci

      # ── PR 状态注释 ──────────────────────────────────
      - name: Comment PR status
        if: always()
        uses: actions/github-script@v7
        with:
          script: |
            const { context, github } = require('@actions/github');
            const status = '${{ job.status }}';
            const emoji = status === 'success' ? '✅' : '❌';
            const body = `## CI 检查结果 ${emoji}\n\n**状态**: ${status}\n\n| 检查项 | 状态 |\n|--------|------|\n| 后端 Lint | ${{ steps.Backend lint.outcome || 'N/A' }} |\n| 后端测试 | ${{ steps.Backend tests.outcome || 'N/A' }} |\n| 前端 Lint | ${{ steps.Frontend lint.outcome || 'N/A' }} |\n| 前端测试 | ${{ steps.Frontend unit tests.outcome || 'N/A' }} |`;
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: body
            });
        continue-on-error: true
