#!/bin/bash
# commit-msg
# Git commit-msg hook that enforces conventional commit format
# Install: cp hooks/commit-msg .git/hooks/commit-msg && chmod +x .git/hooks/commit-msg

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")

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

# Skip revert commits
if echo "$COMMIT_MSG" | grep -qE "^Revert"; then
    exit 0
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🔍 COMMIT MESSAGE VALIDATION"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# ─────────────────────────────────────────────────────────────
# Conventional Commit Format
# ─────────────────────────────────────────────────────────────
# type(scope): description
#
# Valid types:
#   feat     - New feature
#   fix      - Bug fix
#   docs     - Documentation only
#   style    - Formatting, no code change
#   refactor - Code change, no feature/fix
#   perf     - Performance improvement
#   test     - Adding tests
#   build    - Build system changes
#   ci       - CI/CD changes
#   chore    - Maintenance
#   revert   - Revert commit

VALID_TYPES="feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert"

# Pattern: type(scope): description
# - type is required
# - scope is optional (in parentheses)
# - colon and space are required
# - description is required

PATTERN="^($VALID_TYPES)(\([a-z0-9_-]+\))?: .+"

# Get first line (subject)
SUBJECT=$(echo "$COMMIT_MSG" | head -1)

echo "Message: \"$SUBJECT\""
echo ""

# ─────────────────────────────────────────────────────────────
# Validation
# ─────────────────────────────────────────────────────────────

VALID=1
ERRORS=""

# Check 1: Matches conventional format
if ! echo "$SUBJECT" | grep -qE "$PATTERN"; then
    VALID=0
    ERRORS="${ERRORS}  ❌ Does not match format: type(scope): description\n"
fi

# Check 2: Subject length (max 72 characters)
SUBJECT_LENGTH=${#SUBJECT}
if [ $SUBJECT_LENGTH -gt 72 ]; then
    VALID=0
    ERRORS="${ERRORS}  ❌ Subject too long: ${SUBJECT_LENGTH}/72 characters\n"
fi

# Check 3: Subject doesn't end with period
if echo "$SUBJECT" | grep -qE "\.$"; then
    VALID=0
    ERRORS="${ERRORS}  ❌ Subject should not end with a period\n"
fi

# Check 4: Subject starts with lowercase (after type)
if echo "$SUBJECT" | grep -qE "^[a-z]+(\([a-z0-9_-]+\))?: [A-Z]"; then
    ERRORS="${ERRORS}  ⚠️  Description should start with lowercase (optional)\n"
fi

# ─────────────────────────────────────────────────────────────
# Result
# ─────────────────────────────────────────────────────────────

if [ $VALID -eq 0 ]; then
    echo -e "${RED}╔══════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${RED}║              ⛔ INVALID COMMIT MESSAGE ⛔                     ║${NC}"
    echo -e "${RED}╚══════════════════════════════════════════════════════════════╝${NC}"
    echo ""
    echo "Issues found:"
    echo -e "$ERRORS"
    echo ""
    echo "Expected format:"
    echo -e "  ${YELLOW}type(scope): description${NC}"
    echo ""
    echo "Valid types:"
    echo "  feat      New feature"
    echo "  fix       Bug fix"
    echo "  docs      Documentation"
    echo "  style     Formatting"
    echo "  refactor  Code restructure"
    echo "  perf      Performance"
    echo "  test      Tests"
    echo "  build     Build system"
    echo "  ci        CI/CD"
    echo "  chore     Maintenance"
    echo "  revert    Revert"
    echo ""
    echo "Examples:"
    echo "  feat(auth): add login page"
    echo "  fix(api): handle null response"
    echo "  refactor(utils): extract date helpers"
    echo "  docs: update README"
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    exit 1
fi

echo -e "${GREEN}✅ Commit message is valid${NC}"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
exit 0
