name: OpenCode Review

on:
  pull_request:
    types: [opened, synchronize]

concurrency:
  group: opencode-review-${{ github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  opencode-review:
    if: github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false

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

      - name: Install OpenCode
        run: npm install -g opencode-ai@latest

      - name: Review PR
        env:
          OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          opencode run \
            --dangerously-skip-permissions \
            --model openrouter/deepseek/deepseek-v4-pro \
            "Review PR #${PR_NUMBER} on this repository.

            Using the gh CLI:
            1. Run 'gh pr view ${PR_NUMBER} --json title,body' to read the PR description
            2. Run 'gh pr diff ${PR_NUMBER}' to see the code changes
            3. Review for: bugs, security issues, performance, code quality, test coverage
            4. Post your review using 'gh pr comment ${PR_NUMBER} --body \"...\"'

            Be thorough but constructive. Flag critical issues prominently."