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

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

# Get current branch name
current_branch=$(git symbolic-ref --short HEAD 2>/dev/null || echo "detached")

# Get repository name to check if this is wamp-ai or wamp-cicd (infrastructure repos)
repo_name=$(basename "$(git rev-parse --show-toplevel)" 2>/dev/null || echo "unknown")

# Check if committing directly to master/main (AI assistants should use feature branches)
# EXCEPTION: wamp-ai and wamp-cicd infrastructure repos can commit to main/master
if [ "$current_branch" = "main" ] || [ "$current_branch" = "master" ]; then
    if [ "$repo_name" != "wamp-ai" ] && [ "$repo_name" != "wamp-cicd" ]; then
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo "ERROR: Commit rejected - AI assistants must NOT commit to $current_branch"
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        echo "Per AI_POLICY.md and project workflow:"
        echo "  • AI assistants should work on feature branches, not master/main"
        echo "  • Use descriptive branch names (e.g., fix-issue-123, add-feature-xyz)"
        echo "  • This enforces the branch-per-feature GitHub Flow workflow"
        echo ""
        echo "To fix this:"
        echo "  1) Create a feature branch:"
        echo "     git checkout -b feature-branch-name"
        echo ""
        echo "  2) Commit your changes:"
        echo "     git commit -m \"your commit message\""
        echo ""
        echo "  3) Push to bare repo:"
        echo "     git push origin feature-branch-name"
        echo ""
        echo "If you are a HUMAN developer who needs to commit to $current_branch:"
        echo "  • Follow these steps to temporarily disable hooks:"
        echo ""
        echo "    1) git config core.hooksPath /dev/null"
        echo "    2) git commit -m \"your message\""
        echo "    3) git config core.hooksPath .ai/.githooks"
        echo ""
        exit 1
    fi
fi

# 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
