#!/bin/bash
#
# Pre-commit hook for Barn
#
# This hook runs code quality checks before allowing a commit.
# To install: git config core.hooksPath .githooks
#
# To bypass (use sparingly): git commit --no-verify
#

set -e

echo "Running pre-commit checks..."

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

# Track if any check fails
FAILED=0

# Get list of staged Java files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.java$' || true)

if [ -z "$STAGED_FILES" ]; then
    echo -e "${GREEN}No Java files staged, skipping checks.${NC}"
    exit 0
fi

echo "Checking staged Java files..."

# ==============================================================================
# CHECKSTYLE
# ==============================================================================

echo -e "\n${YELLOW}Running Checkstyle...${NC}"
if mvn checkstyle:check -q 2>/dev/null; then
    echo -e "${GREEN}✓ Checkstyle passed${NC}"
else
    echo -e "${RED}✗ Checkstyle failed${NC}"
    echo "  Run 'mvn checkstyle:check' for details"
    FAILED=1
fi

# ==============================================================================
# COMPILE CHECK
# ==============================================================================

echo -e "\n${YELLOW}Compiling...${NC}"
if mvn compile -q -DskipTests 2>/dev/null; then
    echo -e "${GREEN}✓ Compilation passed${NC}"
else
    echo -e "${RED}✗ Compilation failed${NC}"
    echo "  Run 'mvn compile' for details"
    FAILED=1
fi

# ==============================================================================
# SPOTBUGS (only if compile passed)
# ==============================================================================

if [ $FAILED -eq 0 ]; then
    echo -e "\n${YELLOW}Running SpotBugs...${NC}"
    if mvn spotbugs:check -q 2>/dev/null; then
        echo -e "${GREEN}✓ SpotBugs passed${NC}"
    else
        echo -e "${RED}✗ SpotBugs found issues${NC}"
        echo "  Run 'mvn spotbugs:check' for details"
        echo "  Run 'mvn spotbugs:gui' to view in GUI"
        FAILED=1
    fi
fi

# ==============================================================================
# PMD (only if compile passed)
# ==============================================================================

if [ $FAILED -eq 0 ]; then
    echo -e "\n${YELLOW}Running PMD...${NC}"
    if mvn pmd:check -q 2>/dev/null; then
        echo -e "${GREEN}✓ PMD passed${NC}"
    else
        echo -e "${RED}✗ PMD found issues${NC}"
        echo "  Run 'mvn pmd:check' for details"
        FAILED=1
    fi
fi

# ==============================================================================
# UNIT TESTS (optional, can be slow)
# ==============================================================================

# Uncomment to run tests on every commit
# echo -e "\n${YELLOW}Running tests...${NC}"
# if mvn test -q 2>/dev/null; then
#     echo -e "${GREEN}✓ Tests passed${NC}"
# else
#     echo -e "${RED}✗ Tests failed${NC}"
#     echo "  Run 'mvn test' for details"
#     FAILED=1
# fi

# ==============================================================================
# RESULT
# ==============================================================================

echo ""
if [ $FAILED -eq 1 ]; then
    echo -e "${RED}Pre-commit checks failed!${NC}"
    echo "Fix the issues above or use 'git commit --no-verify' to bypass (not recommended)"
    exit 1
else
    echo -e "${GREEN}All pre-commit checks passed!${NC}"
    exit 0
fi
