#!/usr/bin/env sh

# Set the locale to ensure consistent pattern matching
LC_ALL=C

# Get the list of staged files
staged_files=$(git diff --cached --name-only --diff-filter=ACM)

# Run Biome format and lint on staged files
if [ -n "$staged_files" ]; then
  printf " \033[1;34m💅 Running Biome check --fix on staged files...\033[0m\n"
  pnpm biome check --fix $staged_files
  biome_fix_status=$?

  if [ $biome_fix_status -ne 0 ]; then
    printf " \033[1;31m❌ Error:\033[0m Biome found issues that could not be auto-fixed.\n"
    printf " \033[1;33mPlease fix the errors above and try again.\033[0m\n"
    exit 1
  fi

  # Re-add files after formatting
  printf " \033[1;34m➕ Re-adding formatted files to the commit...\033[0m\n"
  git add $staged_files
else
  printf " \033[1;33m⚠️ No files to check.\033[0m\n"
fi

# Success message
printf " \033[1;32m✅ Success:\033[0m All checks passed. Proceeding with commit.\n"

exit 0
