#!/usr/bin/env bash
# Enforce conventional commit message format.
# Pattern: type(optional-scope): description
# Types: feat, fix, docs, style, refactor, test, chore, ci, perf, build
set -euo pipefail

COMMIT_MSG_FILE="$1"
COMMIT_MSG=$(head -1 "$COMMIT_MSG_FILE")

PATTERN="^(feat|fix|docs|style|refactor|test|chore|ci|perf|build)(\(.+\))?: .{1,72}$"

if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
    echo "ERROR: Commit message does not follow Conventional Commits format."
    echo ""
    echo "Expected: <type>(<scope>): <description>"
    echo "Types: feat, fix, docs, style, refactor, test, chore, ci, perf, build"
    echo ""
    echo "Examples:"
    echo "  feat: add user authentication"
    echo "  fix(api): handle null response"
    echo "  docs: update README with setup instructions"
    echo ""
    echo "Your message: $COMMIT_MSG"
    exit 1
fi
