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

# Get staged .js and .ts files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts)$' | tr '\n' ' ')

if [ -z "$STAGED_FILES" ]; then
  echo "→ No staged files match any configured task."
  exit 0
fi

echo "🔧 Running ESLint with auto-fix on staged files..."
pnpm exec eslint --fix $STAGED_FILES
if [ $? -ne 0 ]; then
  echo "❌ ESLint failed. Please fix the issues before committing."
  exit 1
fi

echo "🔍 Running TypeScript type check..."
pnpm exec tsc --noEmit
if [ $? -ne 0 ]; then
  echo "❌ TypeScript type check failed. Please fix the issues before committing."
  exit 1
fi

# Re-add files that may have been auto-fixed by ESLint
git add $STAGED_FILES

echo "✅ Pre-commit checks passed!"
