#!/bin/bash
#
# Git commit-msg hook to enforce AI_POLICY.md
# Prevents commits with AI co-authorship which violates our policy
#

commit_msg_file="$1"
commit_msg=$(cat "$commit_msg_file")

# Check for prohibited AI co-authorship patterns
# Pattern 1: Standard prose patterns like "authored by Claude", "generated by AI", etc.
if echo "$commit_msg" | grep -i -E "(authored by|co-authored by|generated by).*(claude|gemini|copilot|ai|artificial intelligence)" > /dev/null; then
    echo "ERROR: Commit rejected - violates the project's AI_POLICY.md"
    echo ""
    echo "The commit message contains AI authorship attribution which is prohibited."
    echo "Per AI_POLICY.md, an AI can never be credited as an Author or Co-Author."
    echo "This project _does_ acknowledge AI use, please see AI_ACKNOWLEDGEMENT.md."
    echo ""
    echo "You (the human) are the sole author of this contribution, even when AI was used in assistance."
    echo "Please adjust the commit comment and commit again. An acceptable commit comment may end with (for example):"
    echo ""
    echo '"Note: This work was completed with AI assistance (Claude Code)."'
    echo ""
    exit 1
fi

# Pattern 2: Git trailer format like "Co-Authored-By: Claude" or "Generated-By: AI"
if echo "$commit_msg" | grep -i -E "(co-)?authored-by:|generated-by:|signed-off-by:" | grep -i -E "(claude|gemini|copilot|ai|artificial intelligence)" > /dev/null; then
    echo "ERROR: Commit rejected - violates the project's AI_POLICY.md"
    echo ""
    echo "The commit message contains AI authorship attribution in Git trailer format which is prohibited."
    echo "Per AI_POLICY.md, an AI can never be credited as an Author or Co-Author."
    echo "This project _does_ acknowledge AI use, please see AI_ACKNOWLEDGEMENT.md."
    echo ""
    echo "You (the human) are the sole author of this contribution, even when AI was used in assistance."
    echo "Please adjust the commit comment and commit again. An acceptable commit comment may end with (for example):"
    echo ""
    echo '"Note: This work was completed with AI assistance (Claude Code)."'
    echo ""
    exit 1
fi

# Pattern 3: AI attribution in links or mentions like "Generated with [Claude Code]"
if echo "$commit_msg" | grep -i -E "(generated with|created with|built with|made with).*(claude|gemini|copilot|ai)" > /dev/null; then
    echo "ERROR: Commit rejected - violates the project's AI_POLICY.md"
    echo ""
    echo "The commit message contains AI tool attribution which is prohibited."
    echo "Per AI_POLICY.md, an AI can never be credited as an Author or Co-Author."
    echo "This project _does_ acknowledge AI use, please see AI_ACKNOWLEDGEMENT.md."
    echo ""
    echo "You (the human) are the sole author of this contribution, even when AI was used in assistance."
    echo "Please adjust the commit comment and commit again. An acceptable commit comment may end with (for example):"
    echo ""
    echo '"Note: This work was completed with AI assistance (Claude Code)."'
    echo ""
    exit 1
fi

exit 0
