#!/usr/bin/env bash
# Enforce conventional commit message format.
# Exits 1 if the first line doesn't match the pattern.
# Warns (but allows) if subject > 72 chars.

# Color only when stderr is a terminal
if [ -t 2 ]; then
  RED='\033[0;31m'
  YELLOW='\033[0;33m'
  RESET='\033[0m'
else
  RED=''
  YELLOW=''
  RESET=''
fi

commit_msg_file="$1"
if [ -z "$commit_msg_file" ] || [ ! -f "$commit_msg_file" ]; then
  echo -e "${RED}error: commit message file not found${RESET}" >&2
  exit 1
fi

# Read the first non-comment, non-empty line
subject=""
while IFS= read -r line; do
  # Skip comments (git default) and blank lines
  [[ "$line" =~ ^# ]] && continue
  [[ -z "$line" ]] && continue
  subject="$line"
  break
done < "$commit_msg_file"

if [ -z "$subject" ]; then
  echo -e "${RED}error: commit message is empty${RESET}" >&2
  exit 1
fi

# --- Exempt patterns (pass through without validation) ---

# Merge commits
if [[ "$subject" =~ ^Merge\  ]]; then
  exit 0
fi

# Fixup / squash commits
if [[ "$subject" =~ ^(fixup|squash)!\  ]]; then
  exit 0
fi

# Revert commits generated by git revert
if [[ "$subject" =~ ^Revert\ \" ]]; then
  exit 0
fi

# --- Conventional commit validation ---

conventional_re='^(feat|fix|docs|test|chore|ci|refactor|perf|style|build|revert)(\([a-zA-Z0-9_./-]+\))?!?: .+'

if ! [[ "$subject" =~ $conventional_re ]]; then
  echo -e "${RED}╭───────────────────────────────────────────────────────────╮${RESET}" >&2
  echo -e "${RED}│  Invalid commit message format.                          │${RESET}" >&2
  echo -e "${RED}│                                                          │${RESET}" >&2
  echo -e "${RED}│  Expected conventional commit:                           │${RESET}" >&2
  echo -e "${RED}│    type(scope): description                              │${RESET}" >&2
  echo -e "${RED}│                                                          │${RESET}" >&2
  echo -e "${RED}│  Types: feat fix docs test chore ci refactor             │${RESET}" >&2
  echo -e "${RED}│         perf style build revert                          │${RESET}" >&2
  echo -e "${RED}│                                                          │${RESET}" >&2
  echo -e "${RED}│  Examples:                                               │${RESET}" >&2
  echo -e "${RED}│    feat: add new SC check for unused imports             │${RESET}" >&2
  echo -e "${RED}│    fix(detector): handle empty class bodies              │${RESET}" >&2
  echo -e "${RED}│    feat!: drop legacy pattern refs                       │${RESET}" >&2
  echo -e "${RED}│                                                          │${RESET}" >&2
  echo -e "${RED}│  Your message:                                           │${RESET}" >&2
  printf  "${RED}│    %-55.55s│${RESET}\n" "$subject" >&2
  echo -e "${RED}╰───────────────────────────────────────────────────────────╯${RESET}" >&2
  exit 1
fi

# --- Subject length warning (non-blocking) ---

subject_len=${#subject}
if [ "$subject_len" -gt 72 ]; then
  echo -e "${YELLOW}warning: subject line is ${subject_len} chars (recommended max 72)${RESET}" >&2
fi

exit 0
