echo "🚀 Running pre-push checks..."

# Run build first to ensure everything compiles
echo "🏗️ Building project..."
npm run build || {
    echo "❌ Build failed. Please fix compilation errors before pushing."
    exit 1
}

# Run memory-safe tests to prevent V8 crashes
echo "🧪 Running memory-safe test suite..."
npm run test:safe || {
    echo "❌ Tests failed. Please fix failing tests before pushing."
    exit 1
}

# Run security tests with memory constraints
echo "🔒 Running security validation..."
NODE_OPTIONS="--max-old-space-size=4096" npm run test:security || {
    echo "❌ Security tests failed. Please address security issues before pushing."
    exit 1
}

# Run the blocking production dependency audit gate — fails unless every
# finding is covered by a live, unexpired entry in security-exceptions.json.
echo "🔍 Running production dependency audit gate..."
node scripts/security-audit-gate.js || {
    echo "❌ Security audit gate failed. Fix the dependency or add a reviewed, time-boxed"
    echo "   exception to security-exceptions.json before pushing."
    exit 1
}

echo "✅ All pre-push checks passed! Ready to push."
