#!/usr/bin/env sh

LC_ALL=C

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

# Run Prettier on staged files first
if [ -n "$staged_files" ]; then
  printf " \033[1;34m💅 Running Prettier on staged files...\033[0m\n"
  yarn prettier --write $staged_files

  # 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 format.\033[0m\n"
fi

# Run ESLint with  on staged files after Prettier
printf " \033[1;34m🔍 Running ESLint on staged files...\033[0m\n"
yarn test:lint
eslint_status=$?

# Check ESLint status
if [ $eslint_status -ne 0 ]; then
  printf " \033[1;31m❌ Error:\033[0m ESLint found issues that need to be addressed.\n"
  printf " \033[1;33mPlease fix the errors above and try again.\033[0m\n"
  exit 1
fi

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

exit 0