#!/usr/bin/env bash
# 𝓥𝓲𝓫𝓮𝓬𝓻𝓪𝓯𝓽𝓮𝓭. pre-commit hook
set -e

echo "⚒  Running pre-commit checks..."

STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)

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

SH_FILES=()
PY_FILES=()
MD_YAML_FILES=()

while IFS= read -r file; do
    [ -z "$file" ] && continue
    case "$file" in
        *.sh|*.bash|*.zsh) SH_FILES+=("$file") ;;
        *.py) PY_FILES+=("$file") ;;
        *.md|*.yaml|*.yml) MD_YAML_FILES+=("$file") ;;
    esac
done <<< "$STAGED_FILES"

if [ ${#SH_FILES[@]} -gt 0 ]; then
    echo "▸ shellcheck (${#SH_FILES[@]} files)"
    env -u PYTHONPATH -u PYTHONHOME scripts/project-python scripts/check_shell.py "${SH_FILES[@]}"
fi

if [ ${#PY_FILES[@]} -gt 0 ]; then
    echo "▸ ruff (${#PY_FILES[@]} files)"
    if command -v uvx >/dev/null 2>&1; then
        # Same pin as pre-push: bare `uvx ruff` formats with each host's cached
        # version, and the pre-push gate then disagrees across machines.
        RUFF_SPEC="$(bash scripts/lib/ruff-spec.sh)"
        uvx "$RUFF_SPEC" check --fix "${PY_FILES[@]}"
        uvx "$RUFF_SPEC" format "${PY_FILES[@]}"
        git add "${PY_FILES[@]}"
    else
        echo "[warn] uvx unavailable; skipping ruff auto-fix"
    fi
fi

if [ ${#MD_YAML_FILES[@]} -gt 0 ]; then
    echo "▸ prettier (${#MD_YAML_FILES[@]} files)"
    # Same pin as pre-push, so the formatter and the gate agree.
    npx --yes "$(bash scripts/lib/prettier-spec.sh)" --write "${MD_YAML_FILES[@]}"
    git add "${MD_YAML_FILES[@]}"
fi

echo "▸ host-paths"
# Same account-home family as payload-hygiene: /Users/<account> and
# /home/<account>. Generic roots (/Users, /home) are not identity. Fail-closed
# — a missing scanner or a hit refuses the commit, not the release.
if ! git diff --cached --name-only --diff-filter=ACMR -z \
    | env -u PYTHONPATH -u PYTHONHOME \
      scripts/project-python scripts/payload_hygiene.py --host-paths --null; then
    echo "FATAL: staged text names a host account path; refuse the commit" >&2
    exit 1
fi

echo "▸ semgrep"
# Staged-only, same contract as shellcheck/ruff above. A Living Tree often
# has unrelated dirty files; a whole-repo scan attributes
# those findings to this commit and blocks it.
# Auto configuration requires telemetry. The standard CI security pack plus
# Rust rules block findings without promoting the broader Python audit pack
# (which needs contextual review) into a commit gate. Registry metrics stay off.
SEMGREP_ARGS=(scan --config p/ci --config p/rust --metrics off --error --quiet --max-target-bytes 3000000 --exclude-rule html.security.audit.missing-integrity.missing-integrity)
# A worker's VIBECRAFTED_ROOT/python-site on PYTHONPATH poisons Homebrew
# pysemgrep (jsonschema/rpds clash). Isolate the scanner.
if command -v semgrep >/dev/null 2>&1; then
    git diff --cached --name-only --diff-filter=ACMR -z | \
      env -u PYTHONPATH -u PYTHONHOME SEMGREP_SEND_METRICS=off xargs -0 semgrep "${SEMGREP_ARGS[@]}" --
elif command -v uvx >/dev/null 2>&1; then
    git diff --cached --name-only --diff-filter=ACMR -z | \
      env -u PYTHONPATH -u PYTHONHOME SEMGREP_SEND_METRICS=off xargs -0 uvx semgrep "${SEMGREP_ARGS[@]}" --
else
    echo "[warn] semgrep unavailable; skipping"
fi

echo "✓ Pre-commit passed"
