#!/bin/bash
# =============================================================================
# COMMIT-MSG HOOK — pinky-streamlit
# =============================================================================
# Format: <emoji> <type>(<scope>): <description>
#   scope  (optional) : setup | core | ui | demo | docs | ci | tests
#   ticket (optional) : Closes: #42 | Refs: #42
#
# Special case: 💾 save: <description> [skip ci]
# =============================================================================

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>(<scope>): <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="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="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="move" ;;
    ⚡️) allowed_type="perf" ;;
    ✅) 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="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(<scope>): <description>"
    exit 1
fi

# Validate full format
SCOPES="setup|core|ui|demo|docs|ci|tests"
TYPES="feat|fix|docs|style|refactor|move|perf|test|revert|build|chore|release|breaking"
PATTERN="^.+ (${TYPES})(\((${SCOPES})\))?: .{3,}"

if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
    echo "❌ Invalid commit format."
    echo ""
    echo "   Format : <emoji> <type>(<scope>): <description>"
    echo "   Scopes : setup | core | ui | demo | docs | ci | tests"
    echo ""
    echo "   Examples:"
    echo "     ✨ feat(core): add load_analytic with cache_data"
    echo "     🐛 fix(ui): handle empty DataFrame in render_detail"
    echo "     🔧 chore(ci): add ruff pre-commit hook"
    echo "     📝 docs(core): add docstring to load_analytic"
    echo "     💾 save: core/page.py in progress [skip ci]"
    exit 1
fi

exit 0
