#!/usr/bin/env bash
# AegisSight-Verwaltung Pre-Commit-Hook.
#
# Ueberprueft bei jeder Aenderung in src/shared/ den Drift-Stand gegen das
# Monitor-Repo. Gibt eine Warnung aus, BLOCKIERT den Commit aber NICHT -
# der User entscheidet selbst, ob er zurueck will.
#
# Installation: scripts/install-hooks.sh

REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT" || exit 0

# Nur prüfen wenn shared/ geändert wird
if ! git diff --cached --name-only | grep -q '^src/shared/'; then
    exit 0
fi

# venv-Python finden
PY=""
for cand in "venv/bin/python3" "venv/bin/python" "/home/claude-dev/.venvs/verwaltung/bin/python3"; do
    if [ -x "$cand" ]; then PY="$cand"; break; fi
done
if [ -z "$PY" ] || [ ! -f "scripts/sync_shared.py" ]; then
    # Tool nicht verfuegbar - silent durchlassen
    exit 0
fi

if ! out=$("$PY" scripts/sync_shared.py --check 2>&1); then
    # --check Exit 1 = auto-syncbarer Drift vorhanden
    echo ""
    echo "================================================================"
    echo " WARNUNG: src/shared/ Drift gegen Monitor-Repo erkannt"
    echo "================================================================"
    echo "$out" | head -30
    echo ""
    echo " Mehr Details: ./venv/bin/python scripts/sync_shared.py --check"
    echo " Aufloesen:    ./venv/bin/python scripts/sync_shared.py --apply"
    echo " (oder bewusst forken: LOCKED_FILES in scripts/sync_shared.py)"
    echo ""
    echo " Commit laeuft trotzdem durch - du entscheidest."
    echo "================================================================"
fi

exit 0
