#!/bin/sh
#
# Pre-commit hook to run tests and code quality checks
# Installed by 'make install-hooks'

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

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

# Store the exit status of each command
status=0

# Check if any files are staged
if git diff --cached --quiet; then
    echo "${YELLOW}No files staged for commit. Skipping pre-commit checks.${NC}"
    exit 0
fi

# Run format check
echo "${YELLOW}Checking code formatting...${NC}"
make check-format
if [ $? -ne 0 ]; then
    echo "${RED}Code formatting check failed.${NC}"
    echo "Run 'make format' to format your code."
    status=1
fi

# Run clippy
echo "${YELLOW}Running linter...${NC}"
make lint
if [ $? -ne 0 ]; then
    echo "${RED}Linting failed.${NC}"
    status=1
fi

# Run unit tests
echo "${YELLOW}Running unit tests...${NC}"
make test-unit
if [ $? -ne 0 ]; then
    echo "${RED}Unit tests failed.${NC}"
    status=1
fi

# Run integration tests
echo "${YELLOW}Running integration tests...${NC}"
make test-integration
if [ $? -ne 0 ]; then
    echo "${RED}Integration tests failed.${NC}"
    status=1
fi

# Print summary
if [ $status -eq 0 ]; then
    echo "${GREEN}All pre-commit checks passed!${NC}"
else
    echo "${RED}Pre-commit checks failed. Commit aborted.${NC}"
    echo "${YELLOW}You can bypass this check with 'git commit --no-verify'${NC}"
fi

exit $status
