#!/usr/bin/env bash
# ops-deploy-fix-build-trigger — PostToolUse hook on Bash, gated by `if: Bash(npm run build:*)`.
# Detects local build failures (npm run build:all / build:*:local / build-queue.sh) and
# dispatches a Haiku build-fixer. Single-flight per repo; budget-capped; transient detection.
set -e
PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}"
. "$PLUGIN_ROOT/scripts/lib/deploy-fix-common.sh"

is_enabled monitor_build_failures || exit 0

INPUT=$(cat)
cmd=$(printf '%s' "$INPUT" | jq -r '.tool_input.command // ""' 2>/dev/null)
exit_code=$(printf '%s' "$INPUT" | jq -r '.tool_response.exitCode // .tool_response.exit_code // 0' 2>/dev/null)
output=$(printf '%s' "$INPUT" | jq -r '.tool_response.output // .tool_response.stdout // ""' 2>/dev/null)

case "$cmd" in
  *"npm run build:"*|*"bash scripts/build-queue.sh"*|*"bash ./scripts/build-queue.sh"*) ;;
  *) exit 0 ;;
esac

# Failure detection
failed=0
[ "$exit_code" != "0" ] && [ -n "$exit_code" ] && failed=1
echo "$output" | tail -120 | grep -qE \
'fastlane (FAIL|failed)|❌|✖  Abort|error TS|FAIL  |Error: AI|Build FAILED|RuntimeError|exit code [^0]|npm error|prepare-build-branch.*Abort' \
  && failed=1
[ "$failed" -ne 1 ] && exit 0

last_lines=$(echo "$output" | tail -120)

# Repo slug and org are read from env (set via Doppler / shell profile) so this
# hook stays project-agnostic and Rule-0 clean.
repo_slug="${OPS_DEPLOY_FIX_REPO_SLUG:-}"
repo_org="${OPS_DEPLOY_FIX_REPO_ORG:-}"
if [ -z "$repo_slug" ]; then
  notify "Build failed" "OPS_DEPLOY_FIX_REPO_SLUG not set — cannot dispatch fixer. Set it in your shell profile or Doppler."
  exit 0
fi
# `|| true` — locate_repo returns 1 when the checkout is absent. Without it the
# plain assignment leaks that non-zero status and `set -e` aborts the hook
# before the fallback below (and the transient/dispatch paths) can run.
repo_path=""
[ -n "$repo_org" ] && repo_path=$(locate_repo "${repo_org}/${repo_slug}" 2>/dev/null) || true
[ -z "$repo_path" ] && repo_path="$HOME/${repo_slug}"
branch=$(git -C "$repo_path" branch --show-current 2>/dev/null || echo "unknown")

# Transient → tell user to retry, no agent
if [ "$(config auto_rerun_transients true)" = "true" ] && is_transient "$last_lines"; then
  notify "Build transient" "Detected transient ($cmd) — recommend retry"
  cat <<JSON
{
  "suppressOutput": true,
  "hookSpecificOutput": {
    "hookEventName": "PostToolUse",
    "additionalContext": "[ops-deploy-fix] '${cmd:0:60}' failed with a transient signature (network/registry/runner blip). No fixer dispatched — retry the build."
  }
}
JSON
  exit 0
fi

# Dedup: same failure tail twice = skip
if already_seen "$repo_slug-build" "$last_lines"; then
  notify "Duplicate build failure" "$cmd same root cause — fixer NOT re-dispatched"
  exit 0
fi

if [ "$(config auto_dispatch_fixer true)" != "true" ]; then
  notify "Build failed" "$cmd — auto_dispatch_fixer=false, manual intervention"
  exit 0
fi

set +e
fix_log=$(dispatch_fix_agent "build-fixer" "$repo_slug-build" \
  "COMMAND=$cmd" "REPO_PATH=$repo_path" "BRANCH=$branch" "LOGS=$last_lines")
dispatch_status=$?
set -e
case $dispatch_status in
  0)
    cat <<JSON
{
  "suppressOutput": true,
  "hookSpecificOutput": {
    "hookEventName": "PostToolUse",
    "additionalContext": "[ops-deploy-fix] Build failure detected in '${cmd:0:60}'. Haiku build-fixer dispatched (logs: $fix_log). Will open fix PR or report transient."
  }
}
JSON
    ;;
  2|3) notify "Auto-fix skipped" "$repo_slug — already in flight or budget exhausted" ;;
esac
exit 0
