#!/usr/bin/env bash
# Pre-commit hook: Check documentation updates
#
# Installation:
#   ln -sf ../../.claude/scripts/pre-commit-docs-check .git/hooks/pre-commit
#   chmod +x .git/hooks/pre-commit
#
# Or copy this file to .git/hooks/pre-commit

set -eo pipefail

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

echo -e "${BLUE}=== Pre-commit: Documentation Check ===${NC}\n"

# Find the project root (where .git directory is)
PROJECT_ROOT=$(git rev-parse --show-toplevel)
DOCS_CHECKER="$PROJECT_ROOT/.claude/scripts/check-docs-updated.sh"

# Check if the documentation checker exists
if [ ! -f "$DOCS_CHECKER" ]; then
    echo -e "${YELLOW}Warning: Documentation checker not found at $DOCS_CHECKER${NC}"
    echo "Skipping documentation check..."
    exit 0
fi

# Make sure it's executable
if [ ! -x "$DOCS_CHECKER" ]; then
    chmod +x "$DOCS_CHECKER"
fi

# Get the main branch name (try 'main' first, fall back to 'master')
MAIN_BRANCH="main"
if ! git rev-parse --verify main >/dev/null 2>&1; then
    if git rev-parse --verify master >/dev/null 2>&1; then
        MAIN_BRANCH="master"
    fi
fi

echo "Checking documentation updates against: $MAIN_BRANCH"
echo ""

# Run the documentation checker
# Note: The script exits with 0 even when warnings are found (informational mode)
output=$("$DOCS_CHECKER" "$MAIN_BRANCH" 2>&1)
exit_code=$?

# Display the output
echo "$output"
echo ""

# Check if there were warnings
if echo "$output" | grep -q "WARNING\|⚠"; then
    echo -e "${YELLOW}╔════════════════════════════════════════════════════════╗${NC}"
    echo -e "${YELLOW}║  Documentation warnings detected!                     ║${NC}"
    echo -e "${YELLOW}║                                                        ║${NC}"
    echo -e "${YELLOW}║  This is informational - your commit will proceed.    ║${NC}"
    echo -e "${YELLOW}║  Consider updating documentation before pushing.      ║${NC}"
    echo -e "${YELLOW}╚════════════════════════════════════════════════════════╝${NC}"
    echo ""
    echo "To make this check blocking, edit this hook and uncomment the 'exit 1' line below."
    echo ""

    # OPTIONAL: Uncomment the line below to make this check blocking
    # This will prevent commits if documentation is missing
    # exit 1
fi

# Always allow commit to proceed (informational mode)
echo -e "${GREEN}✓ Documentation check complete${NC}"
echo ""
exit 0
