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

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

ALL_FILES=$(git ls-files)

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

# pre-push is a pure GATE: it verifies formatting (ruff/prettier --check) but
# never rewrites or re-stages. Auto-formatting belongs to pre-commit, which runs
# on the staged set. Rewriting the full working tree here would mutate
# uncommitted files unrelated to the push (e.g. a dirty doc you never staged).
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 <<< "$ALL_FILES"

echo "▸ vc_frame pane targeting guard"
# The spawn libraries live under the core package. The old root path matched
# nothing and 2>/dev/null hid it, so this guard passed blind; an empty target
# now refuses the push instead.
pane_guard_dir="vibecrafted-core/vibecrafted_core/runtime/scripts/lib"
if ! compgen -G "$pane_guard_dir/*.sh" >/dev/null; then
    printf 'pre-push: pane targeting guard found no spawn libraries under %s\n' "$pane_guard_dir" >&2
    exit 1
fi
leaks="$(
    grep -nE 'vc_frame[^|]*action new-pane' "$pane_guard_dir"/*.sh \
        | grep -vE 'tab-id|tab_id|--floating|# OPERATOR_TAB_OK' \
        || true
)"
if [[ -n "$leaks" ]]; then
    printf 'pre-push: vc_frame new-pane without --tab-id / --floating / OPERATOR_TAB_OK marker:\n%s\n' "$leaks" >&2
    exit 1
fi

if [ ${#SH_FILES[@]} -gt 0 ]; then
    echo "▸ shellcheck (full)"
    python3 scripts/check_shell.py --require-shellcheck "${SH_FILES[@]}"
fi

if [ ${#PY_FILES[@]} -gt 0 ]; then
    echo "▸ ruff (full, check-only)"
    # Pass explicit *.py paths. ruff 0.16+ includes Markdown by default and
    # would format-check fenced examples in docs (that is a prettier lane).
    if command -v uvx >/dev/null 2>&1; then
        # One ruff for every host. Bare `uvx ruff` resolves whatever version
        # each host has cached, so the same tree passed on one machine and
        # failed on another. The pin lives in pyproject.toml.
        RUFF_SPEC="$(bash scripts/lib/ruff-spec.sh)"
        uvx "$RUFF_SPEC" check "${PY_FILES[@]}"
        uvx "$RUFF_SPEC" format --check "${PY_FILES[@]}"
    else
        echo "[warn] uvx unavailable; skipping ruff checks"
    fi
fi

if [ ${#MD_YAML_FILES[@]} -gt 0 ]; then
    echo "▸ prettier (full, check-only)"
    # One prettier for every host; the pin lives in scripts/lib/prettier-spec.sh.
    npx --yes "$(bash scripts/lib/prettier-spec.sh)" --check "${MD_YAML_FILES[@]}"
fi

echo "▸ semgrep (full)"
# A worker's VIBECRAFTED_ROOT/python-site on PYTHONPATH poisons Homebrew
# pysemgrep (jsonschema/rpds clash), exactly as it does in pre-commit — which
# has carried this isolation for a while. Without it the scanner dies with
# `ModuleNotFoundError: No module named 'rpds.rpds'` and the push fails on a
# broken gate rather than on a finding. A security gate that crashes is a
# security gate people learn to --no-verify past, so isolate the interpreter.
SEMGREP_ARGS=(scan --config auto --error --quiet --max-target-bytes 3000000 --exclude-rule html.security.audit.missing-integrity.missing-integrity)
if command -v semgrep >/dev/null 2>&1; then
    env -u PYTHONPATH -u PYTHONHOME semgrep "${SEMGREP_ARGS[@]}"
elif command -v uvx >/dev/null 2>&1; then
    env -u PYTHONPATH -u PYTHONHOME uvx semgrep "${SEMGREP_ARGS[@]}"
else
    echo "[warn] semgrep unavailable; skipping"
fi

echo "✓ Pre-push passed"
