name: CI/CD Pipeline

on:
  push:
    branches: [ master, platform-auto-test ]
  pull_request:
    branches: [ master ]
  workflow_dispatch:
    inputs:
      environment:
        description: 'Test environment'
        required: true
        default: 'test'
        type: choice
        options:
          - test
          - staging
          - production

env:
  PYTHON_VERSION: '3.11'
  NODE_VERSION: '18'

jobs:
  # ==================== 后端测试 ====================
  backend-test:
    name: Backend Tests
    runs-on: windows-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: 'pip'

      - name: Install dependencies
        working-directory: ./backend
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest pytest-asyncio pytest-cov

      - name: Install Playwright
        working-directory: ./backend
        run: |
          playwright install chromium

      - name: Run tests
        working-directory: ./backend
        env:
          DATABASE_URL: sqlite+aiosqlite:///./test_db.sqlite
        run: |
          pytest tests/ -v --cov=app --cov-report=xml --cov-report=html

      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: ./backend/coverage.xml
          fail_ci_if_error: false

  # ==================== 前端测试 ====================
  frontend-test:
    name: Frontend Tests
    runs-on: windows-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'
          cache-dependency-path: ./frontend/package-lock.json

      - name: Install dependencies
        working-directory: ./frontend
        run: npm ci

      - name: Run linter
        working-directory: ./frontend
        run: npm run lint || true

      - name: Build
        working-directory: ./frontend
        run: npm run build

  # ==================== 集成测试 ====================
  integration-test:
    name: Integration Tests
    runs-on: windows-latest
    needs: [backend-test, frontend-test]
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install dependencies
        working-directory: ./backend
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          playwright install chromium

      - name: Start backend
        working-directory: ./backend
        run: |
          Start-Process python -ArgumentList "-m", "uvicorn", "app.main:app", "--host", "127.0.0.1", "--port", "8001"
          Start-Sleep -Seconds 10

      - name: Run integration tests
        working-directory: ./backend
        run: |
          python scripts/test_websocket_flow.py

  # ==================== 构建 ====================
  build:
    name: Build
    runs-on: windows-latest
    needs: [backend-test, frontend-test]
    if: github.event_name == 'push'

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Build Backend
        working-directory: ./backend
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Build Frontend
        working-directory: ./frontend
        run: |
          npm ci
          npm run build

      - name: Archive production artifacts
        uses: actions/upload-artifact@v4
        with:
          name: production-build
          path: |
            frontend/dist/
            backend/

  # ==================== 部署（仅 master 分支）====================
  deploy:
    name: Deploy
    runs-on: windows-latest
    needs: [build, integration-test]
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    environment: ${{ github.event.inputs.environment || 'test' }}

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          name: production-build

      - name: Deploy notification
        run: |
          echo "Deploying to ${{ github.event.inputs.environment || 'test' }} environment"
          echo "Deployment would happen here in a real scenario"

      # 实际部署步骤需要根据具体部署环境配置
      # - name: Deploy to server
      #   run: |
      #     # 部署脚本