#!/usr/bin/env bash
# Lorekeeper pre-push hook
# Slow checks that are too expensive for every commit.
# Runs: branch guard, proposal issue check, mypy, unit tests, E2E tests.
#
# Install: scripts/setup.sh  (run once per clone)

set -euo pipefail

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

echo "🔒  Checking push targets..."

BLOCKED=0

# Pre-push stdin format: <local-ref> <local-sha> <remote-ref> <remote-sha>
while read -r local_ref local_sha remote_ref remote_sha; do
    ref_name="${remote_ref#refs/heads/}"

    if [ "$ref_name" = "main" ] || [ "$ref_name" = "master" ]; then
        echo "🚫  BLOCKED: Push to '$ref_name' is not allowed."
        echo "    All changes to main must go through a pull request (squash-merge)."
        echo "    Push your branch and open a PR instead."
        echo ""
        BLOCKED=1
    fi
done

if [ "$BLOCKED" -eq 1 ]; then
    exit 1
fi

echo "✅  push targets ok"

echo ""
echo "🎫  Checking proposal GitHub issues..."
uv run python scripts/check_issues.py
echo "✅  all proposals reference valid issues"

echo ""
echo "🔍  mypy..."
uv run mypy src
echo "✅  mypy passed"

echo ""
echo "🧪  Unit tests..."
uv run pytest tests/ -q -x --tb=short
echo "✅  unit tests passed"

echo ""
echo "🎭  E2E tests (Playwright / Chromium)..."
uv run pytest tests/e2e/ -m e2e -q --tb=short
echo "✅  E2E passed"

echo ""
echo "✅  All checks passed — pushing"
