name: pr-management

on:
  pull_request_target:
    types: [opened]

jobs:
  check-duplicates:
    runs-on: blacksmith-4vcpu-ubuntu-2404
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
        with:
          fetch-depth: 1

      - name: Check team membership
        id: team-check
        run: |
          LOGIN="${{ github.event.pull_request.user.login }}"
          if [ "$LOGIN" = "opencode-agent[bot]" ] || grep -qxF "$LOGIN" .github/TEAM_MEMBERS; then
            echo "is_team=true" >> "$GITHUB_OUTPUT"
            echo "Skipping: $LOGIN is a team member or bot"
          else
            echo "is_team=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Setup Bun
        if: steps.team-check.outputs.is_team != 'true'
        uses: ./.github/actions/setup-bun

      - name: Install dependencies
        if: steps.team-check.outputs.is_team != 'true'
        run: bun install

      - name: Install opencode
        if: steps.team-check.outputs.is_team != 'true'
        run: curl -fsSL https://kilo.ai/install | bash

      - name: Build prompt
        if: steps.team-check.outputs.is_team != 'true'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          {
            echo "Check for duplicate PRs related to this new PR:"
            echo ""
            echo "CURRENT_PR_NUMBER: $PR_NUMBER"
            echo ""
            echo "Title: $(gh pr view "$PR_NUMBER" --json title --jq .title)"
            echo ""
            echo "Description:"
            gh pr view "$PR_NUMBER" --json body --jq .body
          } > pr_info.txt

      - name: Check for duplicate PRs
        if: steps.team-check.outputs.is_team != 'true'
        env:
          OPENCODE_API_KEY: ${{ secrets.OPENCODE_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          COMMENT=$(bun script/duplicate-pr.ts -f pr_info.txt "Check the attached file for PR details and search for duplicates")

          if [ "$COMMENT" != "No duplicate PRs found" ]; then
            gh pr comment "$PR_NUMBER" --body "_The following comment was made by an LLM, it may be inaccurate:_

          $COMMENT"
          fi

  add-contributor-label:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
      issues: write
    steps:
      - name: Add Contributor Label
        uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
        with:
          script: |
            const isPR = !!context.payload.pull_request;
            const issueNumber = isPR ? context.payload.pull_request.number : context.payload.issue.number;
            const authorAssociation = isPR ? context.payload.pull_request.author_association : context.payload.issue.author_association;

            if (authorAssociation === 'CONTRIBUTOR') {
              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: issueNumber,
                labels: ['contributor']
              });
            }
