#!/bin/bash
# Pre-commit hook - JavaScript/TypeScript code quality checks
# Runs Prettier formatting and ESLint linting on staged JS/TS files

set -e

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

# Get staged JavaScript/TypeScript files
js_ts_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.\(js\|jsx\|ts\|tsx\|mjs\|cjs\)$' || true)

# Skip if no JS/TS files are staged
if [ -z "$js_ts_files" ]; then
    echo -e "${GREEN}✓ No JavaScript/TypeScript files to check${NC}"
    exit 0
fi

echo -e "${YELLOW}Checking staged JavaScript/TypeScript files:${NC}"
echo "$js_ts_files"
echo ""

# Track if any checks fail
checks_failed=0

# ============================================================================
# PRETTIER FORMATTER CHECK
# ============================================================================
echo -e "${YELLOW}Running Prettier formatter check...${NC}"

if command -v prettier &> /dev/null; then
    # Global Prettier
    if prettier --check $js_ts_files 2>&1; then
        echo -e "${GREEN}✓ Prettier formatting passed${NC}"
    else
        echo -e "${RED}✗ Prettier formatting failed${NC}"
        echo -e "${YELLOW}Files need formatting:${NC}"
        prettier --list-different $js_ts_files 2>&1 || true
        echo ""
        echo -e "${YELLOW}To fix: run 'prettier --write .' or 'prettier --write <filename>'${NC}"
        checks_failed=1
    fi
elif [ -f "node_modules/.bin/prettier" ]; then
    # Local Prettier via npm
    if npx prettier --check $js_ts_files 2>&1; then
        echo -e "${GREEN}✓ Prettier formatting passed${NC}"
    else
        echo -e "${RED}✗ Prettier formatting failed${NC}"
        echo -e "${YELLOW}Files need formatting:${NC}"
        npx prettier --list-different $js_ts_files 2>&1 || true
        echo ""
        echo -e "${YELLOW}To fix: run 'npx prettier --write .'${NC}"
        checks_failed=1
    fi
else
    echo -e "${RED}✗ Prettier is not installed${NC}"
    echo -e "${YELLOW}Install with: pnpm add -D prettier${NC}"
    echo -e "${YELLOW}Or globally: pnpm add -g prettier${NC}"
    exit 1
fi

echo ""

# ============================================================================
# ESLINT CHECK
# ============================================================================
echo -e "${YELLOW}Running ESLint check...${NC}"

if command -v eslint &> /dev/null; then
    # Global ESLint
    if eslint $js_ts_files 2>&1; then
        echo -e "${GREEN}✓ ESLint passed${NC}"
    else
        echo -e "${RED}✗ ESLint failed${NC}"
        echo -e "${YELLOW}Linting violations found:${NC}"
        eslint $js_ts_files
        echo ""
        echo -e "${YELLOW}To fix: run 'eslint --fix .' or fix violations manually${NC}"
        checks_failed=1
    fi
elif [ -f "node_modules/.bin/eslint" ]; then
    # Local ESLint via npm
    if npx eslint $js_ts_files 2>&1; then
        echo -e "${GREEN}✓ ESLint passed${NC}"
    else
        echo -e "${RED}✗ ESLint failed${NC}"
        echo -e "${YELLOW}Linting violations found:${NC}"
        npx eslint $js_ts_files
        echo ""
        echo -e "${YELLOW}To fix: run 'npx eslint --fix .'${NC}"
        checks_failed=1
    fi
else
    echo -e "${YELLOW}⚠ ESLint is not installed (optional but recommended)${NC}"
    echo -e "${YELLOW}Install with: pnpm add -D eslint${NC}"
fi

echo ""

# ============================================================================
# TYPESCRIPT TYPE CHECK (if TypeScript files present)
# ============================================================================
ts_files=$(echo "$js_ts_files" | grep '\.\(ts\|tsx\)$' || true)
if [ -n "$ts_files" ]; then
    if [ -f "tsconfig.json" ]; then
        echo -e "${YELLOW}Running TypeScript type check...${NC}"

        if command -v tsc &> /dev/null; then
            if tsc --noEmit 2>&1; then
                echo -e "${GREEN}✓ TypeScript type check passed${NC}"
            else
                echo -e "${RED}✗ TypeScript type check failed${NC}"
                checks_failed=1
            fi
        elif [ -f "node_modules/.bin/tsc" ]; then
            if npx tsc --noEmit 2>&1; then
                echo -e "${GREEN}✓ TypeScript type check passed${NC}"
            else
                echo -e "${RED}✗ TypeScript type check failed${NC}"
                checks_failed=1
            fi
        fi
        echo ""
    fi
fi

# ============================================================================
# FINAL RESULTS
# ============================================================================
if [ $checks_failed -eq 1 ]; then
    echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${RED}Pre-commit checks failed!${NC}"
    echo -e "${RED}Please fix the issues above before committing.${NC}"
    echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    exit 1
else
    echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${GREEN}✓ All pre-commit checks passed!${NC}"
    echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    exit 0
fi
