# Run PHP auto-fixing first
if command -v composer &> /dev/null; then
  # Get list of staged PHP files
  files=$(git diff --cached --name-only --diff-filter=d | grep '\.php$' || true)
  
  if [ -n "$files" ]; then
    composer lint:fix
    # Only re-stage the files that were originally staged
    echo "$files" | xargs git add
    # Then run the check
    composer lint
  fi
else
  echo "Composer is not installed. Skipping PHP linting."
fi
# Em dash lint check
EMDASH_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(html|md|txt|php|module|install|inc|yml|yaml|twig|js|css)$' || true)
if [ -n "$EMDASH_FILES" ]; then
  EMDASH_FOUND=""
  for f in $EMDASH_FILES; do
    if git show ":$f" 2>/dev/null | grep -qP '\xe2\x80\x94' 2>/dev/null || git show ":$f" 2>/dev/null | grep -q "$(printf '\xe2\x80\x94')" 2>/dev/null; then
      EMDASH_FOUND="${EMDASH_FOUND}  ${f}
"
    fi
  done
  if [ -n "$EMDASH_FOUND" ]; then
    echo ""
    echo "Error: Em dashes found in staged files:"
    printf "$EMDASH_FOUND"
    echo "Replace with ' -- ' or rephrase."
    exit 1
  fi
fi
