#!/usr/bin/env bash
# Pre-commit hook: fast format check only (~sub-second).
# Full preflight (clippy, tests, deny, machete) runs in pre-push.
#
# Set up once per clone with:
#   git config core.hooksPath .githooks
#
# Bypass with `--no-verify` or `SKIP_PREFLIGHT=1`.
set -euo pipefail

if [[ "${SKIP_PREFLIGHT:-0}" == "1" ]]; then
    printf '\033[33m[pre-commit] SKIP_PREFLIGHT=1 — skipping\033[0m\n' >&2
    exit 0
fi

cd "$(git rev-parse --show-toplevel)"

if ! command -v just &>/dev/null; then
    # shellcheck disable=SC2016  # backticks are literal text, not expansion
    printf '\033[31m[pre-commit] `just` not found — install it (brew install just)\033[0m\n' >&2
    exit 1
fi

printf '\033[36m[pre-commit] just fmt-check\033[0m\n' >&2
just fmt-check || {
    # shellcheck disable=SC2016  # backticks are literal text, not expansion
    printf '\033[31m[pre-commit] FAILED: run `just fmt` and recommit\033[0m\n' >&2
    exit 1
}
