#!/usr/bin/env bash
# .git/hooks/pre-commit — blocks commit unless quality gate passes
# Install: cp pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit

set -e

echo "⏳ Running quality gate..."

# Detect which package we're in
if [ -d "actiongate" ]; then PKG="actiongate"
elif [ -d "budgetgate" ]; then PKG="budgetgate"
elif [ -d "rulegate" ]; then PKG="rulegate"
elif [ -d "auditgate" ]; then PKG="auditgate"
else echo "⚠️  Unknown package directory, skipping gate"; exit 0
fi

echo "📦 Package: $PKG"

# Tests
echo "🧪 pytest..."
python3 -m pytest tests/ -q --tb=short || { echo "❌ Tests failed. Commit blocked."; exit 1; }

# Type check
echo "🔍 mypy..."
python3 -m mypy "$PKG/" --strict --no-error-summary 2>/dev/null || { echo "❌ Type check failed. Commit blocked."; exit 1; }

# Lint
echo "🧹 ruff..."
python3 -m ruff check "$PKG/" --quiet || { echo "❌ Lint failed. Commit blocked."; exit 1; }

# No debug prints
if grep -rn "print(" "$PKG/" --include="*.py" | grep -v "# noqa" | grep -v "def.*print" > /dev/null 2>&1; then
  echo "⚠️  Warning: print() found in source. Remove or add # noqa."
fi

# No TODOs
if grep -rn "TODO\|FIXME\|HACK\|XXX" "$PKG/" --include="*.py" > /dev/null 2>&1; then
  echo "⚠️  Warning: TODO/FIXME found in source."
fi

echo "✅ Quality gate passed."
