#!/usr/bin/env bash
# Enforces commit message format:
#   Human: <type>(<scope>): <subject>
#   AI:    🤖 <type>(<scope>): <subject>
# Allowed types: feat|fix|chore|docs|refactor|ci
# Merge/revert commits bypass the check.

COMMIT_MSG_FILE="$1"
MSG=$(head -1 "$COMMIT_MSG_FILE")

TYPES="feat|fix|chore|docs|refactor|ci"

# Allow git-generated merge and revert commits
if echo "$MSG" | grep -qE "^(Merge |Revert )"; then
  exit 0
fi

# Allow human format:  <type>(<scope>): <subject>
# Allow AI format:     🤖 <type>(<scope>): <subject>
if echo "$MSG" | grep -qP "^(🤖 )?($TYPES)(\(.+\))?: .+"; then
  exit 0
fi

echo ""
echo "  INVALID COMMIT MESSAGE"
echo ""
echo "  Got:      $MSG"
echo ""
echo "  Expected:"
echo "    Human:  <type>(<scope>): <subject>"
echo "    AI:     🤖 <type>(<scope>): <subject>"
echo ""
echo "  Types: feat | fix | chore | docs | refactor | ci"
echo "  Scope: optional, e.g. feat(auth): ..."
echo ""
echo "  Examples:"
echo "    feat: add drift detection"
echo "    fix(gate): evaluator skips empty checklist"
echo "    🤖 chore: merge story/e3-s01"
echo ""
exit 1
