#!/bin/sh

if [ "$SKIP_HOOKS" = "1" ]; then
    echo "⏭️  SKIP_HOOKS is set. Bypassing pre-commit checks..."
    exit 0
fi

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo "${BLUE}⏳ Running MovieBox-Tui pre-commit checks...${NC}\n"

echo "${YELLOW}🦀 1/3 Checking Code Formatting (cargo fmt)...${NC}"
if ! cargo fmt --check; then
    echo "\n${RED}❌ Error: Code formatting failed!${NC}"
    echo "${RED}Please run 'cargo fmt' to automatically fix the formatting before committing.${NC}"
    exit 1
fi
echo "${GREEN}✅ Code formatting is perfect.${NC}\n"

echo "${YELLOW}🦀 2/3 Checking Linter Warnings (cargo clippy)...${NC}"
if ! cargo clippy --all-targets --locked -- -D warnings; then
    echo "\n${RED}❌ Error: Clippy found warnings or errors in your code!${NC}"
    echo "${RED}Please fix the warnings above before committing.${NC}"
    exit 1
fi
echo "${GREEN}✅ No clippy warnings found.${NC}\n"
if command -v mdbook >/dev/null 2>&1; then
    echo "${YELLOW}📚 3/3 Checking Documentation Build (mdbook)...${NC}"
    cp CHANGELOG.md docs/changelog.md 2>/dev/null || true
    cp CONTRIBUTING.md docs/contributing.md 2>/dev/null || true
    if ! mdbook build >/dev/null 2>&1; then
        echo "\n${RED}❌ Error: mdBook build failed!${NC}"
        exit 1
    fi
    echo "${GREEN}✅ Documentation build passed.${NC}\n"
fi

echo "${GREEN}🎉 All checks passed! Committing...${NC}"
exit 0
