#!/bin/bash
# gxpm pre-commit hook
# Installed by `gxpm-init --install-hooks`. Reads `.gxpm/issues/<id>/state.json`
# to enforce phase gate. No-op if no state file or no issue id in branch name.
# Escape hatch: GXPM_GATE_DISABLE=1 git commit ...

set -e

if [ -x "./bin/gxpm" ]; then
  GXPM_BIN="./bin/gxpm"
elif command -v gxpm >/dev/null 2>&1; then
  GXPM_BIN="$(command -v gxpm)"
else
  exit 0
fi

"$GXPM_BIN" gate branch-policy

current_branch="$(git symbolic-ref --short HEAD 2>/dev/null || echo '')"
issue_id=$(printf '%s' "$current_branch" | grep -oEi '(gxg|gxpm)-[0-9]+' | head -1 | tr 'a-z' 'A-Z')

if [ -z "$issue_id" ]; then
  exit 0
fi

if [ ! -f ".gxpm/issues/${issue_id}/state.json" ]; then
  exit 0
fi

staged_files=()
while IFS= read -r -d '' staged_file; do
  staged_files+=("$staged_file")
done < <(git diff --cached --name-only --diff-filter=ACMR -z)

if [ "${#staged_files[@]}" -eq 0 ]; then
  exit 0
fi

exec "$GXPM_BIN" gate pre-commit "$issue_id" --staged "${staged_files[@]}"
