#!/usr/bin/env bash
# Get inline code review comments from a GitHub PR
# Usage: gh-pr-review-comments <PR_NUMBER> [REPO]
#
# If REPO is not provided, uses the current git remote origin

set -euo pipefail

PR_NUMBER="${1:?Usage: gh-pr-review-comments <PR_NUMBER> [REPO]}"
REPO="${2:-}"

# If no repo provided, try to detect from git remote
if [[ -z "$REPO" ]]; then
  REPO=$(gh repo view --json nameWithOwner -q '.nameWithOwner' 2>/dev/null || true)
  if [[ -z "$REPO" ]]; then
    echo "Error: Could not detect repo. Provide REPO as second argument (e.g., owner/repo)" >&2
    exit 1
  fi
fi

gh api "repos/${REPO}/pulls/${PR_NUMBER}/comments" | jq -r '
  .[] | 
  "───────────────────────────────────────────────────────────────────────
File: \(.path):\(.line // .original_line // "N/A")
Author: \(.user.login) (\(.created_at | split("T")[0]))
───────────────────────────────────────────────────────────────────────
\(.body)
"'
