#!/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.
#
# Bash pur, zéro subprocess (2026-07-22). Les deux `python3 -c` d'avant se justifiaient par « macOS
# BSD grep lacks PCRE (-P) », mais ils ne faisaient pas de PCRE : ils découpaient les deux premiers
# tokens. `read` le fait nativement, et bash porte l'ERE avec `[[ =~ ]]` — aucun `-P` en jeu.
# Ça enlève la dernière dépendance de ce hook à un interpréteur : `python3` existe sur macOS mais
# n'est PAS garanti sur une machine Windows vanilla (ADR-0045 : `py -3` y est le lanceur canonique),
# donc le frère n'aurait pas pu committer. Zéro interpréteur > choisir le bon lanceur.
# Équivalence vérifiée sur 10 messages, emoji multi-octets à sélecteur de variation (♿️, ⚡️) et cas
# limites (vide, sans emoji, mauvaise casse) inclus.
#
# Le découpage sur l'espace est sûr en UTF-8 : l'espace est ASCII et UTF-8 est auto-synchronisant,
# donc un emoji multi-octets n'est jamais coupé en deux.
read -r EMOJI REST <<< "$COMMIT_MSG"

TYPE=""
[[ "$REST" =~ ^([a-z_-]+) ]] && TYPE="${BASH_REMATCH[1]}"

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
