#!/usr/bin/env bash
#
# Pre-push: fast checks for the failure modes that have actually bitten us.
# Runs in ~20-30 seconds. For the full gauntlet (including pytest) run:
#   ./scripts/preflight.sh
#
# One-time setup per clone:
#   git config core.hooksPath .githooks
#
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"

cyan=$'\033[36m'; green=$'\033[32m'; red=$'\033[31m'; off=$'\033[0m'
say()  { printf "%s→%s %s\n" "$cyan" "$off" "$1"; }
pass() { printf "%s✓%s %s\n" "$green" "$off" "$1"; }
fail() { printf "%s✗%s %s\n" "$red" "$off" "$1" >&2; exit 1; }

# ---- 1. Import smoke ----
# Catches syntax errors and missing-module regressions instantly.
say "import smoke"
python - <<'PY' 2>/dev/null || fail "import failed — syntax error or missing module"
from cartograph import cli, cloud
PY
pass "imports ok"

# ---- 2. Wheel install smoke ----
# The exact failure mode that killed v0.5.29: dogfood path resolution works
# in editable install, fails in a real wheel. Rebuild into a tempdir venv
# and confirm cartograph setup --print renders the generated commands.
say "wheel install smoke"
workdir=$(mktemp -d)
trap 'rm -rf "$workdir"' EXIT
python -m venv "$workdir/venv" >/dev/null 2>&1
"$workdir/venv/bin/pip" install --quiet . >/dev/null 2>&1 \
    || fail "pip install . failed"
"$workdir/venv/bin/cartograph" --version >/dev/null 2>&1 \
    || fail "cartograph --version failed after wheel install"
"$workdir/venv/bin/cartograph" setup --print >/dev/null 2>&1 \
    || fail "cartograph setup --print failed — check dogfood path resolution in wheel layout"
pass "wheel install smoke ok"

printf "\n%s✓ pre-push checks passed%s (full pytest: run %s./scripts/preflight.sh%s)\n" \
    "$green" "$off" "$cyan" "$off"
