#!/bin/bash

# Script to check for AI attribution in commits and code
# This enforces the no-AI-attribution policy

set -euo pipefail

LEGACY_AI_PATTERN="c""laude"

echo "Checking for AI attribution..."

# Check recent commits for AI assistant attribution (includes legacy patterns we block)
echo "Checking commit messages..."
ATTRIBUTION_FOUND=0

# Check last 20 commits for attribution
if git log --format="%B" -20 | grep -iE "(${LEGACY_AI_PATTERN}|anthropic|generated with|co-authored-by.*${LEGACY_AI_PATTERN}|ai-generated|llm)" > /dev/null 2>&1; then
    echo "Found AI attribution in recent commits:"
    git log --format="%h %s" --grep="${LEGACY_AI_PATTERN}" --grep="Anthropic" --grep="generated" -i -20
    ATTRIBUTION_FOUND=1
fi

# Check staged commit message if any
if [ -f .git/COMMIT_EDITMSG ]; then
    if grep -iE "(${LEGACY_AI_PATTERN}|anthropic|generated with|co-authored-by.*${LEGACY_AI_PATTERN}|ai-generated|llm)" .git/COMMIT_EDITMSG > /dev/null 2>&1; then
        echo "Found AI attribution in staged commit message"
        ATTRIBUTION_FOUND=1
    fi
fi

# Check code files for AI attribution comments
echo "Checking code for AI attribution comments..."
if grep -r --include="*.rs" --include="*.toml" --include="*.md" -iE "(generated by ${LEGACY_AI_PATTERN}|created by ${LEGACY_AI_PATTERN}|ai-generated|llm-generated)" . 2>/dev/null | grep -v "check-attribution.sh" | grep -v "AUTOMATION_AGENTS.md" | grep -v "reviewer_driver.md"; then
    echo "Found AI attribution in code files"
    ATTRIBUTION_FOUND=1
fi

if [ $ATTRIBUTION_FOUND -eq 0 ]; then
    echo "No AI attribution found"
    exit 0
else
    echo ""
    echo "AI attribution detected!"
    echo "Please remove all references to AI assistants or LLM generation."
    echo "All code must appear as human-authored work."
    exit 1
fi
