#!/bin/bash

COMMIT_MSG_FILE=$1
# Read only the first line of the commit message
COMMIT_MSG_HEADER=$(head -n 1 "$COMMIT_MSG_FILE")

# Pattern: type(scope)!: description
# Fixed the ! position and removed quotes from the variable in the comparison
REG_EXP='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|release)(\([a-z0-9-]+\))?!?: .+'

if [[ ! $COMMIT_MSG_HEADER =~ $REG_EXP ]]; then
    echo "------------------------------------------------------------------"
    echo "❌ ERROR: Invalid commit message format!"
    echo "------------------------------------------------------------------"
    echo "Subject line: $COMMIT_MSG_HEADER"
    echo ""
    echo "Accepted format: <type>(optional-scope): <description>"
    echo "Example: feat(auth)!: add OIDC support"
    echo "------------------------------------------------------------------"
    exit 1
fi

echo "✅ Commit message follows Conventional Commits standard."
exit 0