#!/bin/bash

# Pre-commit hook to prevent AI-related references in commits
# This hook checks for references to Claude, Anthropic, AI, and related terms

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

# Define patterns to check (case-insensitive)
PATTERNS=(
    "claude"
    "anthropic"
    "\\bai\\b"
    "artificial intelligence"
    "machine learning"
    "\\bllm\\b"
    "language model"
    "gpt"
    "openai"
    "copilot"
)

# Get list of staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)

# Function to check file for patterns
check_file() {
    local file=$1
    local found=0
    
    # Skip binary files
    if file -b "$file" | grep -q "binary"; then
        return 0
    fi
    
    # Skip license files - they contain legitimate uses of these terms
    if [[ "$file" == *"/licenses/"* ]] || [[ "$file" == *".txt" ]]; then
        return 0
    fi
    
    for pattern in "${PATTERNS[@]}"; do
        if grep -i -n "$pattern" "$file" > /dev/null 2>&1; then
            if [ $found -eq 0 ]; then
                echo -e "${RED}ERROR: Found prohibited AI-related references in $file:${NC}"
                found=1
            fi
            echo -e "${YELLOW}  Pattern '$pattern' found at:${NC}"
            grep -i -n "$pattern" "$file" | head -3
        fi
    done
    
    return $found
}

# Check commit message
COMMIT_MSG_FILE=".git/COMMIT_EDITMSG"
if [ -f "$COMMIT_MSG_FILE" ]; then
    for pattern in "${PATTERNS[@]}"; do
        if grep -i "$pattern" "$COMMIT_MSG_FILE" > /dev/null 2>&1; then
            echo -e "${RED}ERROR: Commit message contains prohibited AI-related reference: '$pattern'${NC}"
            echo -e "${YELLOW}Please remove all AI-related references from your commit message.${NC}"
            exit 1
        fi
    done
fi

# Check staged files
ERRORS=0
for file in $STAGED_FILES; do
    # Skip this hook file itself and the claude.md file
    if [[ "$file" == *".githooks/pre-commit"* ]] || [[ "$file" == "claude.md" ]]; then
        continue
    fi
    
    # Check if file exists (might be deleted)
    if [ -f "$file" ]; then
        check_file "$file"
        if [ $? -eq 1 ]; then
            ERRORS=1
        fi
    fi
done

if [ $ERRORS -eq 1 ]; then
    echo -e "${RED}"
    echo "=========================================="
    echo "COMMIT BLOCKED: AI-related references found"
    echo "=========================================="
    echo -e "${NC}"
    echo "Please remove all references to Claude, Anthropic, AI, and related terms"
    echo "from your code, comments, and commit messages before committing."
    echo ""
    echo "If this is a false positive, you can bypass this check with:"
    echo "  git commit --no-verify"
    echo ""
    exit 1
fi

exit 0