#!/bin/bash
set -e

# Configuration
REPO="suno-ai/app-android"
PR_NUMBER=1438
BOT_LOGIN="claude[bot]"

echo "Fetching review comments for PR #${PR_NUMBER}..."

# Get all review comments from the PR
COMMENTS=$(gh api \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  "/repos/${REPO}/pulls/${PR_NUMBER}/comments" \
  --jq ".[] | select(.user.login == \"${BOT_LOGIN}\") | {id: .id, node_id: .node_id, body: .body[0:100], path: .path, line: .line}")

if [ -z "$COMMENTS" ]; then
  echo "No comments found from ${BOT_LOGIN}"
  exit 0
fi

echo "Found comments from ${BOT_LOGIN}:"
echo "$COMMENTS" | jq -r '"  - Comment #\(.id) on \(.path):\(.line) - \(.body)..."'

echo ""
read -p "Do you want to minimize these comments? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  echo "Aborted."
  exit 0
fi

echo ""
echo "Minimizing comments..."

# Process each comment
echo "$COMMENTS" | jq -r '.node_id' | while read -r NODE_ID; do
  echo "  Minimizing comment with node_id: ${NODE_ID}"

  gh api graphql -f query='
    mutation {
      minimizeComment(input: {
        subjectId: "'"${NODE_ID}"'",
        classifier: OUTDATED
      }) {
        minimizedComment {
          isMinimized
          minimizedReason
        }
      }
    }
  ' > /dev/null

  if [ $? -eq 0 ]; then
    echo "    ✓ Minimized successfully"
  else
    echo "    ✗ Failed to minimize"
  fi
done

echo ""
echo "✅ Done! Check the PR: https://github.com/${REPO}/pull/${PR_NUMBER}"
