#!/bin/bash
# Commit-msg hook for Atmosphere Framework
# Enforces two-line commit message format and prevents AI-generated messages

set -e

COMMIT_MSG_FILE=$1

# Read the commit message
commit_msg=$(cat "$COMMIT_MSG_FILE")

# Remove comments (lines starting with #)
commit_msg_clean=$(echo "$commit_msg" | grep -v '^#' || true)

# Count non-empty lines
line_count=$(echo "$commit_msg_clean" | grep -v '^$' | wc -l | tr -d ' ')

# Get first line
first_line=$(echo "$commit_msg_clean" | head -1)

# Check for AI-generated signatures
if echo "$commit_msg" | grep -qiE "Generated with \[Claude|Co-Authored-By: Claude|Co-authored-by: Copilot|Generated by AI|AI-assisted|Created with Claude|Generated with Claude Code|Co-authored-by:.*noreply.github.com"; then
    echo "ERROR: AI-generated commit trailer detected!"
    echo ""
    echo "Found AI signature in commit message. Please write a proper commit message without:"
    echo "  - 'Generated with [Claude Code]' or similar"
    echo "  - 'Co-Authored-By: Claude' or other AI attribution"
    echo "  - 'Co-authored-by: Copilot' or similar bot trailers"
    echo ""
    exit 1
fi

# Check for AI-attributed author field (complements the trailer check above).
# Setting git config user.name/user.email to an AI identity bypasses the
# trailer check since no "Co-authored-by:" line is needed. Block specific
# known AI identities; do not block legitimate human names or CI bots.
AUTHOR_NAME=$(git var GIT_AUTHOR_IDENT | sed -E 's/ <.*//')
AUTHOR_EMAIL=$(git var GIT_AUTHOR_IDENT | sed -E 's/.*<([^>]+)>.*/\1/')
if echo "$AUTHOR_NAME" | grep -qiE '^(claude|claude code|copilot|github copilot)$' \
        || echo "$AUTHOR_EMAIL" | grep -qiE '(noreply@anthropic\.com|Copilot@users\.noreply\.github\.com)$'; then
    echo "ERROR: AI-attributed author field detected: $AUTHOR_NAME <$AUTHOR_EMAIL>"
    echo ""
    echo "Commits must be attributed to a human contributor. Set your git identity:"
    echo "  git config user.name \"Your Name\""
    echo "  git config user.email \"your-email@example.com\""
    echo ""
    echo "Then re-commit (the message itself is fine, only the author field needs fixing)."
    exit 1
fi

# Validate format: must be 1-2 non-empty lines only
if [ "$line_count" -eq 0 ]; then
    echo "ERROR: Empty commit message!"
    echo ""
    echo "Please provide a commit message."
    exit 1
fi

if [ "$line_count" -gt 2 ]; then
    echo "ERROR: Commit message too long!"
    echo ""
    echo "Found $line_count lines. Maximum allowed: 2 lines."
    echo ""
    echo "Correct format:"
    echo "   Line 1: Brief summary (50-72 chars recommended)"
    echo "   Line 2: Optional detailed description (if needed)"
    echo ""
    echo "Your commit message:"
    echo "$commit_msg_clean" | head -5 | sed 's/^/   /'
    if [ "$line_count" -gt 5 ]; then
        echo "   ... ($((line_count - 5)) more lines)"
    fi
    echo ""
    echo "Tip: Keep it concise!"
    exit 1
fi

# Validate first line length (recommended: 50-72 chars, max: 100)
first_line_length=${#first_line}
if [ "$first_line_length" -gt 100 ]; then
    echo "ERROR: First line too long!"
    echo ""
    echo "First line is $first_line_length characters. Maximum: 100 characters."
    echo ""
    echo "Your first line:"
    echo "   $first_line"
    echo ""
    echo "Tip: Keep the summary line concise and under 72 characters."
    exit 1
fi

# Validate first line starts with conventional commit type (optional but recommended)
if ! echo "$first_line" | grep -qE '^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?: '; then
    echo "Warning: First line doesn't follow conventional commit format"
    echo ""
    echo "Recommended format: type(scope): description"
    echo "Examples:"
    echo "  feat: add user authentication"
    echo "  fix: resolve database connection issue"
    echo "  docs: update API documentation"
    echo ""
    echo "Continuing anyway..."
    echo ""
fi

# If we get here, commit message is valid
echo "Commit message format valid ($line_count line(s))"
exit 0
