#!/usr/bin/env bash

# Replies to a PR review thread. Body is read from stdin to avoid
# shell escaping issues with markdown (quotes, newlines, etc.).
#
# GraphQL rather than REST (`POST pulls/{n}/comments/{id}/replies`) because the
# surrounding pipeline is thread-node-ID-native: get-pr-comments returns PRRT_
# IDs and resolveReviewThread has no REST equivalent, so replying by thread ID
# keeps one ID currency end to end. REST addresses a *comment*, which would mean
# carrying the thread's root comment databaseId purely for this call. Both
# produce a real inline threaded reply -- this is not a capability difference.
#
# Omitting pullRequestReviewId below means the reply is absorbed into the
# viewer's PENDING review when one exists, returning success while staying
# invisible until that draft is submitted. The modes guard against this via
# get-pr-comments' pending_review key; do not add a reviewId here without one.

set -e

if [ $# -lt 1 ]; then
    echo "Usage: echo 'reply body' | reply-to-pr-thread THREAD_ID"
    echo "Example: echo 'Addressed: added null check' | reply-to-pr-thread PRRT_kwDOABC123"
    exit 1
fi

THREAD_ID=$1
BODY=$(cat)

if [ -z "$BODY" ]; then
    echo "Error: No body provided on stdin."
    exit 1
fi

gh api graphql -f threadId="$THREAD_ID" -f body="$BODY" -f query='
mutation ReplyToReviewThread($threadId: ID!, $body: String!) {
  addPullRequestReviewThreadReply(input: {
    pullRequestReviewThreadId: $threadId
    body: $body
  }) {
    comment {
      id
      url
    }
  }
}'
