#!/bin/sh

# Pre-commit hook for charivo project
# This hook runs ESLint and Prettier checks on staged files only

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo "${YELLOW}🔍 Running pre-commit checks...${NC}"

# Get the root directory of the git repository
REPO_ROOT=$(git rev-parse --show-toplevel)
cd "$REPO_ROOT"

# Check if pnpm is available
if ! command -v pnpm >/dev/null 2>&1; then
    echo "${RED}❌ pnpm is not installed or not in PATH${NC}"
    exit 1
fi

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

if [ -z "$STAGED_FILES" ]; then
    echo "${GREEN}✅ No staged files to check${NC}"
    exit 0
fi

# Filter JS/TS files for linting
JS_TS_FILES=$(echo "$STAGED_FILES" | grep -E '\.(js|jsx|ts|tsx|mjs|cjs)$' || true)
# Filter all files for formatting (Prettier can handle many file types)
FORMATTABLE_FILES=$(echo "$STAGED_FILES" | grep -E '\.(js|jsx|ts|tsx|mjs|cjs|json|md|yml|yaml|css|scss|html)$' || true)

HAS_ERRORS=0

# Run ESLint on JS/TS files
if [ -n "$JS_TS_FILES" ]; then
    echo "${YELLOW}🔧 Running ESLint on staged JS/TS files...${NC}"
    
    # Create a temporary file list for eslint
    TEMP_ESLINT_LIST=$(mktemp)
    echo "$JS_TS_FILES" > "$TEMP_ESLINT_LIST"
    
    if ! pnpm eslint --no-error-on-unmatched-pattern $(echo "$JS_TS_FILES" | tr '\n' ' '); then
        echo "${RED}❌ ESLint found issues in staged files${NC}"
        echo "${YELLOW}💡 You can try to fix them automatically with: pnpm lint:fix${NC}"
        HAS_ERRORS=1
    else
        echo "${GREEN}✅ ESLint passed${NC}"
    fi
    
    rm -f "$TEMP_ESLINT_LIST"
fi

# Run Prettier check on formattable files
if [ -n "$FORMATTABLE_FILES" ]; then
    echo "${YELLOW}✨ Checking Prettier formatting on staged files...${NC}"
    
    # Create a temporary file list for prettier
    TEMP_PRETTIER_LIST=$(mktemp)
    echo "$FORMATTABLE_FILES" > "$TEMP_PRETTIER_LIST"
    
    if ! pnpm prettier --check $(echo "$FORMATTABLE_FILES" | tr '\n' ' '); then
        echo "${RED}❌ Some staged files are not properly formatted${NC}"
        echo "${YELLOW}💡 You can fix them automatically with: pnpm format${NC}"
        echo "${YELLOW}💡 Or format only staged files with: git diff --cached --name-only | xargs pnpm prettier --write${NC}"
        HAS_ERRORS=1
    else
        echo "${GREEN}✅ Prettier formatting check passed${NC}"
    fi
    
    rm -f "$TEMP_PRETTIER_LIST"
fi

# Run TypeScript type checking (optional, can be slow)
if [ -n "$JS_TS_FILES" ]; then
    echo "${YELLOW}🔍 Running TypeScript type check...${NC}"
    
    if ! pnpm typecheck >/dev/null 2>&1; then
        echo "${RED}❌ TypeScript type checking failed${NC}"
        echo "${YELLOW}💡 Run 'pnpm typecheck' to see detailed type errors${NC}"
        HAS_ERRORS=1
    else
        echo "${GREEN}✅ TypeScript type check passed${NC}"
    fi
fi

# Summary
if [ $HAS_ERRORS -eq 0 ]; then
    echo "${GREEN}🎉 All pre-commit checks passed!${NC}"
    exit 0
else
    echo "${RED}❌ Pre-commit checks failed. Please fix the issues above and try again.${NC}"
    echo "${YELLOW}💡 Quick fixes:${NC}"
    echo "  - ${YELLOW}pnpm lint:fix${NC} (fix ESLint issues)"
    echo "  - ${YELLOW}pnpm format${NC} (fix formatting)"
    echo "  - ${YELLOW}pnpm typecheck${NC} (check TypeScript errors)"
    exit 1
fi