#!/usr/bin/env sh

# Get list of staged files before formatting
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)

# Run biome check with auto-fix
npm run check

# If biome or tsc failed, abort the commit
if [ $? -ne 0 ]; then
  echo "❌ Pre-commit checks failed. Please fix the errors and try again."
  exit 1
fi

# Re-stage files that were originally staged and may have been formatted
for file in $STAGED_FILES; do
  if [ -f "$file" ]; then
    git add "$file"
  fi
done

echo "✅ Pre-commit checks passed!"