# =============================================================================
# AWOOOI Type Sync Check (Phase 14.3 #99)
# =============================================================================
# 驗證 Pydantic 模型與 TypeScript 型別是否同步
#
# 觸發條件: apps/api/src/models/** 變更
# 失敗時: 開發者需執行 `pnpm --filter @awoooi/shared-types generate`
#
# 2026-03-31 Claude Code (Phase 14.3)

name: Type Sync Check

on:
  push:
    branches: [main]
    paths:
      - 'apps/api/src/models/**'
      - 'packages/shared-types/**'
      - 'scripts/generate-schemas.py'
  pull_request:
    paths:
      - 'apps/api/src/models/**'
      - 'packages/shared-types/**'
      - 'scripts/generate-schemas.py'

jobs:
  check-type-sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        # 2026-04-05 Claude Code: 改用 apt 安裝，避免 setup-python toolcache glibc 版本不符
        run: |
          python3 --version
          pip3 install -q uv 2>/dev/null || (apt-get update -q && apt-get install -y -q python3-pip && pip3 install -q uv)

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Setup pnpm
        uses: pnpm/action-setup@v3
        with:
          version: 9

      - name: Install Python Dependencies
        run: |
          cd apps/api
          uv pip install --system pydantic structlog -q

      - name: Install Node Dependencies
        run: pnpm install --filter @awoooi/shared-types

      - name: Generate Types (Temp)
        run: |
          cd apps/api
          python3 ../../scripts/generate-schemas.py
          echo "=== Generated schema definition count ==="
          python3 -c "import json; d=json.load(open('../../packages/shared-types/schemas/api-types.json')); print(f'definitions: {len(d[\"definitions\"])}')"
          cd ../../packages/shared-types
          pnpm generate:types

      - name: Check for Differences
        run: |
          echo "=== git diff packages/shared-types/ ==="
          git diff packages/shared-types/
          if git diff --exit-code packages/shared-types/; then
            echo "✅ TypeScript 型別與 Pydantic 模型同步"
          else
            echo "❌ TypeScript 型別需要更新！"
            echo ""
            echo "請在本地執行："
            echo "  pnpm --filter @awoooi/shared-types generate"
            echo ""
            echo "然後提交更新的型別檔案。"
            exit 1
          fi
