#!/usr/bin/env bash
# Scope: universal | Validates commit message format (Conventional Commits)
# MORPH-SPEC Commit Message Hook
# Enforces Conventional Commits specification

COMMIT_MSG_FILE="$1"
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")

# Conventional Commits pattern: type(scope): description
# Types: feat, fix, docs, style, refactor, perf, test, chore, build, ci, revert
PATTERN="^(feat|fix|docs|style|refactor|perf|test|chore|build|ci|revert)(\(.+\))?: .{1,100}"

if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
  echo "❌ Invalid commit message format!"
  echo ""
  echo "Required format: <type>(<scope>): <description>"
  echo ""
  echo "Types: feat, fix, docs, style, refactor, perf, test, chore, build, ci, revert"
  echo ""
  echo "Examples:"
  echo "  feat(auth): add JWT refresh token rotation"
  echo "  fix(api): resolve null reference in UserService"
  echo "  docs(readme): update installation instructions"
  echo ""
  echo "Your message:"
  echo "  $COMMIT_MSG"
  echo ""
  echo "Override with: git commit --no-verify"
  exit 1
fi

echo "✅ Commit message format valid"
exit 0
