#!/bin/bash
# Pre-commit hook: Run security regression tests before allowing commit
# Install: cp .githooks/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit

set -e

echo "🔒 Running security regression tests before commit..."
echo ""

# Change to repository root
cd "$(git rev-parse --show-toplevel)/codebase/features/status-dashboard/server"

# Check if node_modules exists
if [ ! -d "node_modules" ]; then
    echo "⚠️  node_modules not found. Installing dependencies..."
    pnpm install
fi

# Run security tests (fast, no coverage)
echo "Running 243 security tests..."
if ! pnpm run test:security; then
    echo ""
    echo "❌ Security tests failed. Commit blocked."
    echo ""
    echo "To fix:"
    echo "  1. Run: pnpm run test:security:watch"
    echo "  2. Fix failing tests"
    echo "  3. Try committing again"
    echo ""
    echo "To bypass (NOT RECOMMENDED):"
    echo "  git commit --no-verify"
    echo ""
    exit 1
fi

echo ""
echo "✅ Security tests passed. Proceeding with commit..."
echo ""

exit 0
