#!/bin/bash
# Pre-commit hook: auto-reorder org frontmatter to canonical field ordering.
# Install: ln -sf ../../.githooks/pre-commit .git/hooks/pre-commit

set -e

# Get list of staged org pages
STAGED_ORGS=$(git diff --cached --name-only --diff-filter=ACM docs/organisations/*.md 2>/dev/null || true)

if [ -n "$STAGED_ORGS" ]; then
    echo "→ Reordering frontmatter in staged org pages..."
    python util/reorder_frontmatter.py

    echo "→ Refreshing proof_level on staged org pages (local/offline, no network)..."
    python util/check_event_sourcing.py --recalculate >/dev/null
    # --recalculate skips any event carrying proof_level_locked: true, so a
    # deliberate manual override survives an unrelated commit to that file.

    # Re-stage any files that were just fixed
    for f in $STAGED_ORGS; do
        git add "$f" 2>/dev/null || true
    done
    echo "→ Done."
fi
