name: PR Artifacts Comment

# This workflow runs after the PR Build workflow completes
# It posts a comment with artifact links to the PR
# This approach avoids permission issues with PRs from forks
on:
  workflow_run:
    workflows: ["PR Build"]
    types: [completed]

permissions:
  actions: read
  pull-requests: write

jobs:
  comment:
    # Only run for successful PR builds
    if: >
      github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Get PR number
        id: pr
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_NUMBER=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} \
            --jq '.pull_requests[0].number')

          if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then
            echo "No PR found for this workflow run"
            exit 0
          fi

          echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
          echo "Found PR #$PR_NUMBER"

      - name: Get artifacts and post comment
        if: steps.pr.outputs.pr_number != ''
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ steps.pr.outputs.pr_number }}
          RUN_ID: ${{ github.event.workflow_run.id }}
        run: |
          # Get artifact list
          ARTIFACTS=$(gh api repos/${{ github.repository }}/actions/runs/$RUN_ID/artifacts \
            --jq '.artifacts[] | "- **\(.name)** (\(.size_in_bytes / 1024 / 1024 | floor) MB)"' | sort)

          if [ -z "$ARTIFACTS" ]; then
            echo "No artifacts found for run $RUN_ID"
            exit 0
          fi

          # Get version info from workflow run name or conclusion
          VERSION=$(gh api repos/${{ github.repository }}/actions/runs/$RUN_ID \
            --jq '.head_branch // "unknown"')

          # Build comment body
          MARKER="<!-- mcpproxy-pr-artifacts -->"
          RUN_URL="https://github.com/${{ github.repository }}/actions/runs/$RUN_ID"

          read -r -d '' COMMENT_BODY << 'ENDOFCOMMENT' || true
          <!-- mcpproxy-pr-artifacts -->
          ## 📦 Build Artifacts

          **Workflow Run:** [View Run](RUN_URL_PLACEHOLDER)
          **Branch:** `VERSION_PLACEHOLDER`

          ### Available Artifacts

          ARTIFACTS_PLACEHOLDER

          ### How to Download

          **Option 1: GitHub Web UI** (easiest)
          1. Go to the workflow run page linked above
          2. Scroll to the bottom "Artifacts" section
          3. Click on the artifact you want to download

          **Option 2: GitHub CLI**
          ```bash
          gh run download RUN_ID_PLACEHOLDER --repo REPO_PLACEHOLDER
          ```

          > **Note:** Artifacts expire in 14 days.
          ENDOFCOMMENT

          # Replace placeholders
          COMMENT_BODY="${COMMENT_BODY//RUN_URL_PLACEHOLDER/$RUN_URL}"
          COMMENT_BODY="${COMMENT_BODY//VERSION_PLACEHOLDER/$VERSION}"
          COMMENT_BODY="${COMMENT_BODY//ARTIFACTS_PLACEHOLDER/$ARTIFACTS}"
          COMMENT_BODY="${COMMENT_BODY//RUN_ID_PLACEHOLDER/$RUN_ID}"
          COMMENT_BODY="${COMMENT_BODY//REPO_PLACEHOLDER/${{ github.repository }}}"
          # Remove leading spaces from heredoc
          COMMENT_BODY=$(echo "$COMMENT_BODY" | sed 's/^          //')

          # Check for existing comment
          EXISTING_COMMENT=$(gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
            --jq ".[] | select(.user.login == \"github-actions[bot]\" and (.body | contains(\"$MARKER\"))) | .id" | head -1)

          if [ -n "$EXISTING_COMMENT" ]; then
            # Update existing comment
            gh api repos/${{ github.repository }}/issues/comments/$EXISTING_COMMENT \
              -X PATCH \
              -f body="$COMMENT_BODY"
            echo "✅ Updated existing artifact comment (ID: $EXISTING_COMMENT)"
          else
            # Create new comment
            gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
              -X POST \
              -f body="$COMMENT_BODY"
            echo "✅ Created new artifact comment"
          fi
