#!/usr/bin/env bash
# Git pre-push hook enforcing Constitution Principle VIII:
# Direct pushes to main are strictly prohibited.
set -e

remote="$1"
url="$2"

while read -r local_ref local_oid remote_ref remote_oid; do
    if [ "$remote_ref" = "refs/heads/main" ] || [ "$remote_ref" = "refs/heads/master" ]; then
        if [ "$ALLOW_MAIN_PUSH" != "1" ]; then
            echo "❌ [Pre-Push Error] Direct push to '$remote_ref' is prohibited by Constitution Principle VIII."
            echo "   Main is a protected branch. All changes must arrive via GitHub Pull Request."
            echo "   Please push your feature branch and open a PR:"
            echo "     git push -u origin \$(git rev-parse --abbrev-ref HEAD)"
            echo "     gh pr create"
            echo "   (Emergency bypass for repository admin only: ALLOW_MAIN_PUSH=1 git push ...)"
            exit 1
        fi
    fi
done

exit 0
