#!/usr/bin/env bash
# Pre-push: heavier checks that catch what CI catches before pushing.
# Runs the full build pipeline so drift, generator bugs, and broken tests
# are caught locally instead of blocking a PR.
# Install: make setup (uses core.hooksPath, no copy needed)
#
# Hook contract (per githooks(5)):
#   args: <remote-name> <remote-url>
#   stdin: lines of "<local-ref> <local-oid> <remote-ref> <remote-oid>"
#          local-ref is "(delete)" for deletions; oid is all-zeroes
#          when the ref does not yet exist on the remote.
#   exit non-zero aborts the push.
set -euo pipefail

# Skip when only deleting refs — nothing to validate.
all_deletes=1
have_input=0
while read -r local_ref _ _ _; do
	have_input=1
	[[ "$local_ref" == "(delete)" ]] || all_deletes=0
done

if [[ $have_input -eq 1 && $all_deletes -eq 1 ]]; then
	echo "pre-push: deletion-only push, skipping checks"
	exit 0
fi

echo "=== Pre-push (full) ==="

# Regenerate derived artifacts
make build || {
	echo "FAIL: make build"
	exit 1
}

# If the build produced drift, auto-commit and ask the user to re-push.
# Happens when src/ was committed with --no-verify or pre-commit wasn't set up.
regen_paths=(dist/ .claude-plugin/marketplace.json .agents/plugins/marketplace.json gemini-extension.json AGENTS.md skills agents hooks)
untracked=$(git ls-files --others --exclude-standard -- dist/ .claude-plugin/ .agents/ 2>/dev/null || true)
if ! git diff --quiet -- "${regen_paths[@]}" || [[ -n "$untracked" ]]; then
	git add -- "${regen_paths[@]}"
	git commit -m "chore: regenerate dist artifacts"
	printf '\nNOTICE: dist was stale — regenerated and committed.\nRe-run git push to include this commit.\n'
	exit 1
fi

# Full test suite
make test || {
	echo "FAIL: make test"
	exit 1
}

make test-ts || {
	echo "FAIL: make test-ts"
	exit 1
}

echo "=== Pre-push passed ==="
