name: API Check

on:
  pull_request:
    branches:
      - main

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

      - name: Check api-spec.yml exists
        run: |
          if [ ! -f "reading-platform-frontend/api-spec.yml" ]; then
            echo "❌ 错误：api-spec.yml 不存在"
            echo "请在后端修改接口后运行：npm run api:update"
            exit 1
          fi
          echo "✅ api-spec.yml 存在"

      - name: Check generated files exist
        run: |
          if [ ! -f "reading-platform-frontend/src/api/generated/api.ts" ]; then
            echo "❌ 错误：src/api/generated/api.ts 不存在"
            echo "请在后端修改接口后运行：npm run api:update"
            exit 1
          fi
          if [ ! -f "reading-platform-frontend/src/api/generated/model.ts" ]; then
            echo "❌ 错误：src/api/generated/model.ts 不存在"
            echo "请在后端修改接口后运行：npm run api:update"
            exit 1
          fi
          echo "✅ API 生成文件存在"

      - name: Check generated files are not empty
        run: |
          if [ ! -s "reading-platform-frontend/src/api/generated/api.ts" ]; then
            echo "❌ 错误：api.ts 文件为空"
            exit 1
          fi
          if [ ! -s "reading-platform-frontend/src/api/generated/model.ts" ]; then
            echo "❌ 错误：model.ts 文件为空"
            exit 1
          fi
          echo "✅ API 生成文件不为空"

      - name: Summary
        run: |
          echo "✅ API 规范检查通过"
          echo "请确保："
          echo "1. 后端修改接口后已运行：npm run api:update"
          echo "2. 已提交 api-spec.yml"
          echo "3. 已提交 src/api/generated/ 下的文件"
