#!/bin/bash
# Edit an existing GitHub pull request's title and/or body using gh CLI.
#
# Usage:
#   edit-pr.sh <pr-number> --title "<new title>"
#   edit-pr.sh <pr-number> --body "<markdown body>"
#   edit-pr.sh <pr-number> --body-file <path-to-markdown-file>
#   edit-pr.sh <pr-number> --title "<new title>" --body-file <path>
#
# At least one of --title, --body, or --body-file is required; --body and
# --body-file are mutually exclusive. Use --body-file for any body longer than
# a few lines — passes the file directly to gh, avoiding shell quoting issues
# with multi-line markdown.
#
# A replacement body must contain the sections docs/GITHUB_WORKFLOW_SPEC.md
# requires (checked here before submission). After the edit, the PR is read
# back from GitHub and compared to what was submitted; the script exits
# nonzero on any mismatch. See lib.sh for why.

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
source "$SCRIPT_DIR/lib.sh"

PR_NUMBER="$1"
shift || true

if [ -z "$PR_NUMBER" ] || [ -z "$1" ]; then
    echo "Error: Missing required arguments" >&2
    echo "Usage: $0 <pr-number> [--title \"<text>\"] [--body \"<text>\" | --body-file <path>]" >&2
    exit 1
fi

if ! command -v gh &> /dev/null; then
    echo "Error: GitHub CLI (gh) is not installed" >&2
    echo "Install: https://cli.github.com/" >&2
    exit 1
fi

TITLE=""
BODY=""
BODY_FILE=""

while [ -n "$1" ]; do
    case "$1" in
        --title)
            TITLE="$2"
            if [ -z "$TITLE" ]; then
                echo "Error: --title requires a non-empty value" >&2
                exit 1
            fi
            shift 2
            ;;
        --body)
            BODY="$2"
            if [ -z "$BODY" ]; then
                echo "Error: --body requires a non-empty value" >&2
                exit 1
            fi
            shift 2
            ;;
        --body-file)
            BODY_FILE="$2"
            if [ -z "$BODY_FILE" ] || [ ! -f "$BODY_FILE" ]; then
                echo "Error: --body-file requires a path to an existing file (got: ${BODY_FILE:-<empty>})" >&2
                exit 1
            fi
            shift 2
            ;;
        *)
            echo "Error: unknown argument: $1" >&2
            echo "Usage: $0 <pr-number> [--title \"<text>\"] [--body \"<text>\" | --body-file <path>]" >&2
            exit 1
            ;;
    esac
done

if [ -n "$BODY" ] && [ -n "$BODY_FILE" ]; then
    echo "Error: --body and --body-file are mutually exclusive" >&2
    exit 1
fi

if [ -z "$TITLE" ] && [ -z "$BODY" ] && [ -z "$BODY_FILE" ]; then
    echo "Error: nothing to edit; pass at least one of --title, --body, or --body-file" >&2
    exit 1
fi

if [ -n "$BODY_FILE" ]; then
    BODY="$(cat "$BODY_FILE")"
fi

if [ -n "$BODY" ]; then
    require_pr_body_sections "$BODY"
fi

ARGS=()
if [ -n "$TITLE" ]; then
    ARGS+=(--title "$TITLE")
fi
if [ -n "$BODY_FILE" ]; then
    ARGS+=(--body-file "$BODY_FILE")
elif [ -n "$BODY" ]; then
    ARGS+=(--body "$BODY")
fi

gh pr edit "$PR_NUMBER" "${ARGS[@]}"

verify_pr_content "$PR_NUMBER" "$TITLE" "$BODY"
