#!/bin/bash
# PostToolUse/Bash hook — after a PR is created from a baton branch
# (<prefix>/<id>-<slug>), remind the agent to comment on the work-item.
# Without comments the board records states but no reasons, and whoever picks the
# item up next has to ask.
#
# Silent unless BOTH hold: a PR-creation command, and a branch carrying an item
# id. Never fails the turn. Needs jq and git.
set -uo pipefail

payload=$(cat)
cmd=$(printf '%s' "$payload" | jq -r '.tool_input.command // ""' 2>/dev/null) || exit 0

# PR creation only — 'gh pr view/list/checks/merge' do not count.
printf '%s' "$cmd" | grep -qE '\bgh[[:space:]]+pr[[:space:]]+create\b' || exit 0

cwd=$(printf '%s' "$payload" | jq -r '.cwd // ""' 2>/dev/null)
[ -d "$cwd" ] || cwd=$PWD

branch=$(git -C "$cwd" branch --show-current 2>/dev/null) || exit 0

# An explicit --head that is not the current branch means the PR is about some other
# branch — a release PR (integration -> production) is the common case, and it bundles
# many items, so pinning its link to whichever item you happen to be standing on
# would be a lie.
head_arg=$(printf '%s' "$cmd" | sed -nE 's/.*(--head|-H)[[:space:]]+([^[:space:]]+).*/\2/p' | sed 's/^[^:]*://')
[ -z "$head_arg" ] || [ "$head_arg" = "$branch" ] || exit 0

# baton-start convention: <prefix>/<id>-<slug>, where <id> is the number baton takes
# (`baton show 42`) written bare or with the board's prefix — `42-` or `CANGURO-42-`.
# Accepts the long forms `feature`/`bugfix` too: the canonical set is the four commit
# types, but a hook that silently ignores a near-miss is worse than a permissive one.
id=$(printf '%s' "$branch" \
     | sed -nE 's#^(feat|feature|fix|bugfix|chore|hotfix)/([A-Za-z]+-)?([0-9]+)-.*#\3#p')
[ -n "$id" ] || exit 0

url=$(printf '%s' "$payload" | jq -r '.tool_response.stdout // ""' 2>/dev/null \
      | grep -oE 'https://[^[:space:]]+/pull/[0-9]+' | head -1)

# No baton, no board reachable, or no such item -> nothing to comment on. Stay quiet:
# this hook runs in every repo, most of which have no board.
command -v baton >/dev/null 2>&1 || exit 0
existing=$(cd "$cwd" && baton show "$id" --comments 2>/dev/null) || exit 0

# The PR link is the one fact the board cannot derive on its own — a board on one
# host knows nothing about a repo on another. Post it; leave the WHY to the agent.
posted=""
if [ -n "$url" ] && ! printf '%s' "$existing" | grep -qF "$url"; then
    if (cd "$cwd" && baton comment "$id" --body "PR opened from \`$branch\` → $url") >/dev/null 2>&1; then
        posted=1
    fi
fi

jq -n --arg id "$id" --arg branch "$branch" --arg url "$url" --arg posted "$posted" '
{
  hookSpecificOutput: {
    hookEventName: "PostToolUse",
    additionalContext: (
      "A PR was created from branch `" + $branch + "` (work-item #" + $id + ")."
      + (if $url != "" then " PR: " + $url else "" end)
      + (if $posted != "" then " The link was posted to the item automatically." else "" end)
      + " Add the part a script cannot write — what it does, what is still open, what blocked you:\n"
      + "  baton comment " + $id + " --body \"<what it does> · <what is still open>\"\n"
      + "Keep it short. `baton show " + $id + " --comments` shows what is already there — do not repeat it."
    )
  }
}'
