name: AI PR Review

on:
  pull_request:
    branches:
      - main
    types: [opened, synchronize, reopened]
    paths-ignore:
      - "**/*.md"
      - "docs/**"
      - ".github/ISSUE_TEMPLATE/**"
      - ".github/release.yml"
      - ".ai-reviewignore"
      - "LICENSE"
      - "deploy/.env.example"
  issue_comment:
    types: [created]
  workflow_dispatch:
    inputs:
      pr_number:
        description: 'PR number to review'
        required: true
        type: number

permissions:
  contents: read
  issues: write
  pull-requests: write

concurrency:
  group: ai-pr-review-${{ github.event_name == 'issue_comment' && github.event.issue.number || github.event_name == 'workflow_dispatch' && inputs.pr_number || github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  ai-review:
    name: AI PR Review
    if: >
      (
        github.event_name == 'pull_request' &&
        github.event.pull_request.head.repo.full_name == github.repository
      ) ||
      github.event_name == 'workflow_dispatch' ||
      (
        github.event_name == 'issue_comment' &&
        github.event.issue.pull_request != null &&
        (
          contains(github.event.comment.body, '/ai-review') ||
          contains(github.event.comment.body, '@ai-reviewer review')
        ) &&
        contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)
      )
    runs-on: ubuntu-latest
    env:
      OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

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

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

      - name: Skip when API key is not configured
        if: ${{ env.OPENAI_API_KEY == '' }}
        run: echo "::notice::OPENAI_API_KEY secret is not configured; skipping AI PR review."

      - name: Run AI PR reviewer
        if: ${{ env.OPENAI_API_KEY != '' }}
        run: npx --yes github-ai-pr-reviewer@latest --post
        env:
          OPENAI_API_KEY: ${{ env.OPENAI_API_KEY }}
          OPENAI_MODEL: ${{ vars.OPENAI_MODEL || 'gpt-5.5' }}
          OPENAI_API_MODE: ${{ vars.OPENAI_API_MODE || 'responses' }}
          # 可选：reasoning 模型推理强度，例如 minimal / low / medium / high / xhigh；留空使用模型默认
          OPENAI_REASONING_EFFORT: ${{ vars.OPENAI_REASONING_EFFORT }}
          # 可选：Responses API reasoning summary，例如 auto / concise / detailed
          OPENAI_REASONING_SUMMARY: ${{ vars.OPENAI_REASONING_SUMMARY }}
          OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL || 'https://api.openai.com/v1' }}
          OPENAI_TIMEOUT_MS: ${{ vars.OPENAI_TIMEOUT_MS || '120000' }}
          OPENAI_RETRIES: ${{ vars.OPENAI_RETRIES || '2' }}
          GITHUB_TOKEN: ${{ github.token }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          PR_NUMBER: ${{ github.event_name == 'issue_comment' && github.event.issue.number || github.event_name == 'workflow_dispatch' && inputs.pr_number || github.event.pull_request.number }}
          PR_HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || '' }}
          REVIEW_RULES: .github/ai-review.md,AGENTS.md
          REVIEW_IGNORE: .ai-reviewignore
          REVIEW_SEVERITY_THRESHOLD: P3
          SUMMARY_MODE: review
          REVIEW_EVENT: AUTO
          REQUEST_CHANGES_ON: P1
          APPROVE_WHEN_CLEAN: 'false'
          GITHUB_TIMEOUT_MS: '30000'
          GITHUB_RETRIES: '2'
          # 可选：发现 P1/P0 时让 CI 失败
          # FAIL_ON: P1
