#!/usr/bin/env sh

# Pre-commit hook: Fast feedback TDD workflow
# Defense in depth: ESLint → Vitest (changed tests) → (PHPStan in CI/CD)
# Goal: <15 seconds for fast developer feedback

echo "🔍 Pre-commit quality gates..."
echo ""

# Gate 1: ESLint (JavaScript/TypeScript linting)
echo "Gate 1/2: ESLint..."
npm run lint
if [ $? -ne 0 ]; then
  echo "❌ ESLint failed. Fix linting errors before committing."
  exit 1
fi
echo "✅ ESLint passed"
echo ""

# Gate 2: Vitest (Frontend unit tests - optimized with --changed)
echo "Gate 2/2: Vitest unit tests (changed tests only)..."
npx vitest run --changed --passWithNoTests
VITEST_EXIT=$?

# If --changed found no tests or failed, run full suite as fallback
if [ $VITEST_EXIT -ne 0 ]; then
  echo "⚠️  Running full test suite as fallback..."
  npm run test
  if [ $? -ne 0 ]; then
    echo "❌ Vitest tests failed. Fix failing tests before committing."
    exit 1
  fi
fi
echo "✅ Vitest tests passed"
echo ""

echo "✅ All pre-commit gates passed!"
echo ""
echo "Note: PHPStan and PHP tests run in CI/CD pipeline"
echo "Tip: For full test suite, run 'npm test' manually"
