name: 🧪 Tests

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  NODE_VERSION: "22"

jobs:
  # ============================================================================
  # 代码质量检查
  # ============================================================================
  lint:
    name: 🔍 Code Quality
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - name: 📥 Checkout code
        uses: actions/checkout@v6

      - name: Setup PNPM
        uses: pnpm/action-setup@v6
        with:
          cache: true

      - name: 📦 Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 📥 Install dependencies
        run: pnpm ci

      - name: 🔍 Run linter
        run: pnpm run lint

      - name: 🔍 Type check
        run: pnpm type-check 2>/dev/null || pnpm exec tsc --noEmit

  # ============================================================================
  # 单元测试 (有测试时运行)
  # ============================================================================
  unit-tests:
    name: 🧪 Unit Tests
    runs-on: ubuntu-latest
    timeout-minutes: 15
    needs: lint

    steps:
      - name: 📥 Checkout code
        uses: actions/checkout@v6

      - name: Setup PNPM
        uses: pnpm/action-setup@v6
        with:
          cache: true

      - name: 📦 Setup Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 📥 Install dependencies
        run: pnpm ci

      - name: 🧪 Run unit tests
        run: pnpm run test:run 2>/dev/null || echo "No test:run script, skipping"

      - name: 📊 Upload test results
        uses: actions/upload-artifact@v7
        if: always()
        with:
          name: unit-test-results-node-${{ env.NODE_VERSION }}
          path: |
            coverage/
            test-results.xml
          retention-days: 7
          if-no-files-found: ignore

  # ============================================================================
  # 构建测试
  # ============================================================================
  build-test:
    name: 🏗️ Build Test
    runs-on: ubuntu-latest
    timeout-minutes: 15
    needs: lint

    steps:
      - name: 📥 Checkout code
        uses: actions/checkout@v6

      - name: Setup PNPM
        uses: pnpm/action-setup@v6
        with:
          cache: true

      - name: 📦 Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 📥 Install dependencies
        run: pnpm ci

      - name: 🏗️ Build project
        run: pnpm run build

      - name: 📦 Upload build artifacts
        uses: actions/upload-artifact@v7
        with:
          name: build-output
          path: |
            .next/
            dist/
          retention-days: 7
          if-no-files-found: ignore

  # ============================================================================
  # 测试总结
  # ============================================================================
  test-summary:
    name: 📋 Test Summary
    runs-on: ubuntu-latest
    needs: [unit-tests, build-test]
    if: always()

    steps:
      - name: 📋 Generate test summary
        run: |
          echo "# 🧪 Test Results Summary" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "## 📊 Test Status" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "| Test Type | Status |" >> $GITHUB_STEP_SUMMARY
          echo "|-----------|--------|" >> $GITHUB_STEP_SUMMARY
          echo "| Unit Tests | ${{ needs.unit-tests.result == 'success' && '✅ Passed' || needs.unit-tests.result == 'skipped' && '⏭️ Skipped' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "| Build Test | ${{ needs.build-test.result == 'success' && '✅ Passed' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "## 📈 Metrics" >> $GITHUB_STEP_SUMMARY
          echo "- **Commit**: \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
          echo "- **Branch**: \`${{ github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY
          echo "- **Workflow**: [\#${{ github.run_number }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY

      - name: 🎉 Success notification
        if: needs.build-test.result == 'success'
        run: |
          echo "🎉 Build and checks passed successfully!" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Ready for deployment! 🚀" >> $GITHUB_STEP_SUMMARY
