# PAC CI
#
# 目前只有一个 job:OpenAPI 文档「漂移闸」。
# 文档站的 /docs/api 由 apps/pac-docs/openapi/pac.json 渲染,而该 spec 从
# pac-service 的代码(nestjs-zod DTO)生成。本闸确保两者同步:
#   改了 API 但忘了重新导出 spec → 文档与真实接口对不上 → CI fail。
#
# 本地修复:  pnpm docs:openapi   然后提交 apps/pac-docs/openapi/pac.json

stages:
  - docs

openapi-drift:
  stage: docs
  image: node:22
  rules:
    # MR 里只在改了 service / types / spec 时跑(省 CI)
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - apps/pac-service/**/*
        - packages/types/**/*
        - apps/pac-docs/openapi/pac.json
    # 默认分支推送总是跑(兜底)
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
  cache:
    key:
      files:
        - pnpm-lock.yaml
    paths:
      - .pnpm-store
  before_script:
    - corepack enable
    - corepack prepare pnpm@10.13.1 --activate
    - pnpm config set store-dir .pnpm-store
    - pnpm install --frozen-lockfile
    # preview 模式不连 DB,但构建 AppModule 需要 prisma client + 一份 env
    - pnpm --filter @pac/service prisma:generate
    - cp apps/pac-service/.env.example apps/pac-service/.env
  script:
    - pnpm docs:openapi
    - |
      if ! git diff --exit-code apps/pac-docs/openapi/pac.json; then
        echo ""
        echo "✖ OpenAPI spec 与代码不同步。"
        echo "  你改了 API(controller / DTO),但没重新导出 spec。"
        echo "  本地执行:  pnpm docs:openapi"
        echo "  然后提交:  apps/pac-docs/openapi/pac.json"
        exit 1
      fi
    - echo "✓ OpenAPI spec 与代码一致"
