#!/usr/bin/env bash
#
# pre-commit hook (repo-managed)
#
# Purpose:
# - Keep VERSION and .claude-plugin/marketplace.json in sync when release metadata is edited.
# - Auto-rebuild the current-platform harness binary when go/ source files are staged.
# - Never auto-bump versions for ordinary code/docs commits.
#
# Install:
#   ./local-scripts/install-git-hooks.sh
#

set -euo pipefail

ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [ -z "$ROOT" ]; then
  exit 0
fi

cd "$ROOT"

# --- Section 1: Release metadata sync ---
# Keep VERSION and marketplace.json in sync when either is staged.

METADATA_FILES=(harness/VERSION .claude-plugin/marketplace.json)
CHANGED_METADATA="$(git diff --cached --name-only -- "${METADATA_FILES[@]}" 2>/dev/null | grep -v '^$' || true)"

if [ -n "$CHANGED_METADATA" ]; then
  if [ -f "./.claude/skills/release-this/scripts/sync-version.sh" ]; then
    if ! bash ./.claude/skills/release-this/scripts/sync-version.sh check >/dev/null 2>&1; then
      echo "[pre-commit] Release metadata mismatch detected."
      echo "             harness/VERSION is the source of truth; syncing marketplace.json to match."
      bash ./.claude/skills/release-this/scripts/sync-version.sh sync
      git add harness/VERSION .claude-plugin/marketplace.json 2>/dev/null || true
      SYNCED_VERSION="$(tr -d '[:space:]' < harness/VERSION)"
      echo "[pre-commit] Version metadata synced to $SYNCED_VERSION"
    fi
  fi
fi

# --- Section 2: Auto-rebuild harness binary on go/ changes ---
# If any Go source file is staged, rebuild the current-platform binary and re-stage it.

CHANGED_GO="$(git diff --cached --name-only 2>/dev/null | grep '^go/' || true)"
if [ -n "$CHANGED_GO" ]; then
  echo "[pre-commit] go/ changes detected — rebuilding all platform binaries…"
  if make build-all >/dev/null 2>&1; then
    git add harness/bin/harness-darwin-arm64 harness/bin/harness-darwin-amd64 harness/bin/harness-linux-amd64 2>/dev/null || true
    echo "[pre-commit] harness/bin/ binaries rebuilt and re-staged"
  else
    echo "[pre-commit] WARNING: binary rebuild failed (go not installed?), continuing without rebuild"
  fi
fi

exit 0
