#!/usr/bin/env bash

update-changelog-with-llm() {
    local num_lines="${1:-100}"

    # Check if CHANGELOG.md exists
    if [[ ! -f "CHANGELOG.md" ]]; then
        echo "Error: CHANGELOG.md not found in the current directory."
        exit 1
    fi

    # Check if there are staged changes
    if [[ -z $(git diff --staged) ]]; then
        echo "Error: No staged changes found. Please stage your changes with 'git add'."
        exit 1
    fi

    # Create tmp directory if it doesn't exist
    mkdir -p tmp

    # Split CHANGELOG.md into the portion we'll send to the LLM and the rest
    local top_file="tmp/changelog_top"
    local rest_file="tmp/changelog_rest"
    head -n "$num_lines" CHANGELOG.md > "$top_file"
    tail -n +"$((num_lines + 1))" CHANGELOG.md > "$rest_file"

    # Create prompt file
    local prompt_file="tmp/llm_prompt"
    echo -e "\nWrite prompt to $prompt_file."
    cat << EOF > "$prompt_file"
Update the top portion of a CHANGELOG.md file based on the git diff provided.

Below is only the first $num_lines line(s) of CHANGELOG.md, not the whole file. The remainder of the file is not shown and must not be referenced or reproduced.

Follow these instructions:
1. Read the provided CHANGELOG.md excerpt below
2. Analyze the git diff to understand what changes were made
3. Update the excerpt to include these changes in the appropriate section (e.g. add an "Unreleased" entry, or a new version section, following Keep a Changelog format: https://keepachangelog.com/en/1.1.0/)
4. Maintain the existing formatting and structure of the excerpt
5. Do not truncate or omit any part of the excerpt that doesn't need to change

Reply with the complete updated excerpt only, in the exact same format as provided, and nothing else (no preamble, no explanation, no code fences).

<<<CHANGELOG.md (top $num_lines lines)>>>
$(cat "$top_file")

<<<git diff --staged>>>
$(git diff --staged)

EOF

    # Run the llm command
    echo -e "Send prompt with CHANGELOG.md excerpt and git diff to LLM and wait for response."
    local result=$(cat "$prompt_file" | llm)

    # Check if result is empty
    if [[ -z "$result" ]]; then
        echo "No response from the model. Exiting."
        exit 1
    fi

    # Save the result to a file
    local result_file="tmp/llm_prompt_result"
    echo "$result" > "$result_file"
    echo -e "Saved response to $result_file.\n"

    # Show a preview of result file
    less "$result_file"

    # Ask for confirmation before updating CHANGELOG.md
    read -p "Do you want to update CHANGELOG.md with these changes? (y/n): " confirm
    if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
        exit 0
    fi

    # Update CHANGELOG.md: new top + untouched rest
    cat "$result_file" "$rest_file" > tmp/changelog_new
    cp tmp/changelog_new CHANGELOG.md
    echo "CHANGELOG.md has been updated."

    # Stage the updated CHANGELOG.md
    git add CHANGELOG.md
    echo "CHANGELOG.md has been staged."

    # Show the final diff
    echo -e "\nFinal changes to be committed:"
    git diff --staged CHANGELOG.md
}
ucwl() {
    update-changelog-with-llm "$@"
}