#!/bin/sh
#
# Conventional Commits enforcement
# Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
# Format: type(scope): description
#         type: description
#
# Examples:
#   feat: add Twitter search tool
#   fix(reddit): handle 403 on private subreddits
#   feat!: redesign auth flow (BREAKING CHANGE)

commit_msg=$(cat "$1")

# Skip merge commits and chore: bump commits from CI
if echo "$commit_msg" | grep -qE '^(Merge|chore: bump version)'; then
  exit 0
fi

pattern='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?!?: .+'

if ! echo "$commit_msg" | head -1 | grep -qE "$pattern"; then
  echo ""
  echo "  INVALID COMMIT MESSAGE"
  echo "  ──────────────────────"
  echo "  Your message: $commit_msg"
  echo ""
  echo "  Expected format: type(scope): description"
  echo ""
  echo "  Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert"
  echo ""
  echo "  Examples:"
  echo "    feat: add Google Trends tool"
  echo "    fix(twitter): handle empty search results"
  echo "    feat!: remove extension dependency (breaking)"
  echo ""
  exit 1
fi
