#!/bin/bash
# Claude MPM GitHub CLI Wrapper
# Customizes PR messages with Claude MPM branding

# Function to modify PR body
modify_pr_body() {
    local body="$1"

    # Replace Claude Code with Claude MPM
    body=$(echo "$body" | sed 's/🤖 Generated with \[Claude Code\](https:\/\/claude\.ai\/code)/🤖👥 Generated with [Claude MPM](https:\/\/github.com\/bobmatnyc\/claude-mpm)/')
    body=$(echo "$body" | sed 's/Generated with \[Claude Code\]/Generated with [Claude MPM]/')
    body=$(echo "$body" | sed 's/Claude Code/Claude MPM/g')

    echo "$body"
}

# Check if this is a PR create operation
if [[ "$1" == "pr" ]] && [[ "$2" == "create" ]]; then
    # Process arguments to modify --body parameter
    args=("$@")
    modified_args=()

    for ((i=0; i<${#args[@]}; i++)); do
        if [[ "${args[$i]}" == "--body" ]]; then
            # Next argument is the body
            modified_args+=("${args[$i]}")
            i=$((i+1))
            if [[ $i -lt ${#args[@]} ]]; then
                modified_body=$(modify_pr_body "${args[$i]}")
                modified_args+=("$modified_body")
            fi
        else
            modified_args+=("${args[$i]}")
        fi
    done

    # Execute gh with modified arguments
    gh "${modified_args[@]}"
else
    # For all other gh commands, pass through unchanged
    gh "$@"
fi