#!/usr/bin/env bash
# Git pre-commit hook enforcing Constitution Principles VI & VII:
# - Principle VI: Max 200 LOC changed and Max 10 files per commit.
# - Principle VII: Direct commits to main/master prohibited (must use feature branches).
set -e

MAX_FILES=10
MAX_LOC=200

echo "🔍 Running pre-commit checks..."

# 0. Check feature branch isolation (Principle VII)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" = "main" ] || [ "$CURRENT_BRANCH" = "master" ]; then
    if [ "$ALLOW_MAIN_COMMIT" != "1" ]; then
        echo "❌ [Pre-Commit Error] Direct commits to '$CURRENT_BRANCH' are prohibited by Constitution Principle VII."
        echo "   Every feature must be developed in a dedicated feature branch."
        echo "   Use Spec Kit (e.g. /speckit-specify or create_new_feature.py) or run:"
        echo "     git checkout -b ###-feature-name"
        echo "   (Emergency bypass for repo maintenance only: ALLOW_MAIN_COMMIT=1 git commit ...)"
        exit 1
    fi
fi

# 1. Check staged file count
STAGED_FILES=$(git diff --cached --name-only)
if [ -z "$STAGED_FILES" ]; then
    exit 0
fi

FILE_COUNT=$(echo "$STAGED_FILES" | wc -l | tr -d ' ')

if [ "$FILE_COUNT" -gt "$MAX_FILES" ]; then
    echo "❌ [Pre-Commit Error] Commit rejected: Too many files changed ($FILE_COUNT files > $MAX_FILES max limit)."
    echo "   Principle VI mandates atomic commits. Please split your changes into smaller commits."
    exit 1
fi

# 2. Check lines of code (LOC) additions + deletions (excluding lockfiles)
LOC_CHANGES=$(git diff --cached --numstat | grep -v -E '(uv\.lock|package-lock\.json|poetry\.lock)' | awk '{add+=$1; del+=$2} END {print add+del}')
LOC_CHANGES=${LOC_CHANGES:-0}

if [ "$LOC_CHANGES" -gt "$MAX_LOC" ]; then
    echo "❌ [Pre-Commit Error] Commit rejected: Commit size too large ($LOC_CHANGES LOC > $MAX_LOC max limit)."
    echo "   Principle VI mandates atomic commits. Please decompose your changes into smaller commits (<= $MAX_LOC LOC)."
    exit 1
fi

# 3. Fast Pyrefly type check if .venv exists
if [ -f ".venv/bin/pyrefly" ]; then
    echo "⚡ Running Pyrefly type check..."
    .venv/bin/pyrefly check
fi

echo "✅ Pre-commit verification passed ($FILE_COUNT files, $LOC_CHANGES LOC on branch '$CURRENT_BRANCH')."
exit 0
