#!/bin/bash

# Pre-commit hook to run Prettier
echo "Running Prettier format check..."

# Check if prettier is available
if ! command -v prettier &> /dev/null; then
    echo "Prettier not found. Please install it with: bun add -d prettier"
    exit 1
fi

# Run prettier check on staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|js|json)$')

if [[ -n "$STAGED_FILES" ]]; then
    echo "Checking format for staged files..."
    
    # Check if files are formatted
    npx prettier --check $STAGED_FILES
    
    if [[ $? -ne 0 ]]; then
        echo ""
        echo "❌ Some files are not formatted correctly."
        echo "Please run 'npm run format' or 'bun run format' to fix formatting issues."
        echo "Or run 'npx prettier --write $STAGED_FILES' to format just the staged files."
        exit 1
    fi
    
    echo "✅ All staged files are properly formatted."
fi

exit 0
