#!/bin/bash
# =============================================================================
# COMMIT-MSG HOOK
# =============================================================================
# Format: <emoji> <type>: <description>
#   ticket (optional) : Closes: #42 | Refs: #42
#
# Special case: 💾 save: <description> [skip ci]
#
# Distributed from pinky-workspace/core/hooks/ — do not edit in place; edit the
# source and re-run setup.py.
#
# No scope group (2026-07-10): a per-repo scope vocabulary (scopes.yaml + a
# scopes-local.txt file in every repo + a generated AGENT.md block) turned out
# to cost more mechanism than the git-log grep value it bought — most local
# scopes were just a finer-grained restatement of a single generic bucket.
# Dropped; `type` is the categorization axis that stays.
# =============================================================================

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

# Skip automatic merges
if echo "$COMMIT_MSG" | grep -qE "^Merge "; then
    exit 0
fi

# Special save case — [skip ci] required
if echo "$COMMIT_MSG" | grep -qE "^💾 save: .{3,} \[skip ci\]"; then
    exit 0
fi

# Emoji → allowed type mapping
# Python3 used for emoji extraction — macOS BSD grep lacks PCRE (-P) support
EMOJI=$(python3 -c "
import sys, re
msg = sys.argv[1]
parts = msg.split(None, 1)
print(parts[0] if parts else '')
" "$COMMIT_MSG" 2>/dev/null)

TYPE=$(python3 -c "
import sys, re
msg = sys.argv[1]
parts = msg.split(None, 2)
if len(parts) >= 2:
    m = re.match(r'^([a-z_-]+)', parts[1])
    if m:
        print(m.group(1))
" "$COMMIT_MSG" 2>/dev/null)

if [ -z "$EMOJI" ] || [ -z "$TYPE" ]; then
    echo "❌ Invalid format — missing emoji or type."
    echo "   Format: <emoji> <type>: <description>"
    exit 1
fi

allowed_type=""
case "$EMOJI" in
    🎉) allowed_type="feat" ;;
    ✨) allowed_type="feat" ;;
    📈) allowed_type="feat" ;;
    🌐) allowed_type="feat" ;;
    🚩) allowed_type="feat" ;;
    🛂) allowed_type="feat" ;;
    👔) allowed_type="feat" ;;
    ♿️) allowed_type="feat" ;;
    🦺) allowed_type="feat" ;;
    🚸) allowed_type="feat" ;;
    📱) allowed_type="feat" ;;
    🥚) allowed_type="feat" ;;
    🔍️) allowed_type="feat" ;;
    🧵) allowed_type="feat" ;;
    🌍) allowed_type="feat" ;;
    🐛) allowed_type="fix" ;;
    🥅) allowed_type="fix" ;;
    🩹) allowed_type="fix" ;;
    🚑️) allowed_type="fix" ;;
    🔒️) allowed_type="fix" ;;
    👽️) allowed_type="fix" ;;
    🚨) allowed_type="fix" ;;
    🍎) allowed_type="fix" ;;
    🐧) allowed_type="fix" ;;
    🏁) allowed_type="fix" ;;
    🤖) allowed_type="fix" ;;
    🍏) allowed_type="fix" ;;
    📝) allowed_type="docs" ;;
    💡) allowed_type="docs" ;;
    💬) allowed_type="docs" ;;
    📄) allowed_type="docs" ;;
    👥) allowed_type="docs" ;;
    🎨) allowed_type="style" ;;
    ✏️) allowed_type="style" ;;
    💄) allowed_type="style" ;;
    💫) allowed_type="style" ;;
    ♻️) allowed_type="refactor" ;;
    🔥) allowed_type="refactor" ;;
    🗑️) allowed_type="refactor" ;;
    ⚰️) allowed_type="refactor" ;;
    🗃️) allowed_type="refactor" ;;
    🏗️) allowed_type="refactor" ;;
    💩) allowed_type="refactor" ;;
    🍻) allowed_type="refactor" ;;
    🚚) allowed_type="move" ;;
    ⚡️) allowed_type="perf" ;;
    ✅) allowed_type="test" ;;
    🧪) allowed_type="test" ;;
    🤡) allowed_type="test" ;;
    📸) allowed_type="test" ;;
    🩺) allowed_type="test" ;;
    ⏪️) allowed_type="revert" ;;
    📦️) allowed_type="build" ;;
    ⬆️) allowed_type="build" ;;
    ⬇️) allowed_type="build" ;;
    ➕) allowed_type="build" ;;
    ➖) allowed_type="build" ;;
    📌) allowed_type="build" ;;
    🔧) allowed_type="chore" ;;
    🔨) allowed_type="chore" ;;
    👷) allowed_type="chore" ;;
    💚) allowed_type="chore" ;;
    🔊) allowed_type="chore" ;;
    🔇) allowed_type="chore" ;;
    🔐) allowed_type="chore" ;;
    🚧) allowed_type="chore" ;;
    🔀) allowed_type="chore" ;;
    🍱) allowed_type="chore" ;;
    🏷️) allowed_type="chore" ;;
    🌱) allowed_type="chore" ;;
    🧱) allowed_type="chore" ;;
    🙈) allowed_type="chore" ;;
    ⚗️) allowed_type="chore" ;;
    💸) allowed_type="chore" ;;
    🚀) allowed_type="release" ;;
    🔖) allowed_type="release" ;;
    💥) allowed_type="breaking" ;;
    *)
        echo "❌ Emoji '$EMOJI' not in mapping."
        echo "   See CLAUDE.md for the full emoji ↔ type table."
        exit 1
        ;;
esac

# Validate emoji matches declared type
if [ "$TYPE" != "$allowed_type" ]; then
    echo "❌ Emoji '$EMOJI' requires type '$allowed_type', got '$TYPE'."
    echo "   Use: $EMOJI $allowed_type: <description>"
    exit 1
fi

# Validate full format
TYPES="feat|fix|docs|style|refactor|move|perf|test|revert|build|chore|release|breaking"
PATTERN="^.+ (${TYPES}): .{3,}"

if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
    echo "❌ Invalid commit format."
    echo ""
    echo "   Format : <emoji> <type>: <description>"
    echo ""
    echo "   Examples:"
    echo "     ✨ feat: add manifest variable resolution"
    echo "     🐛 fix: handle missing warehouse field"
    echo "     🔧 chore: add release workflow"
    echo "     📝 docs: add plan command usage"
    echo "     💾 save: core/manifest.py in progress [skip ci]"
    exit 1
fi

exit 0
