#!/usr/bin/env bash
# Enforces Conventional Commits 1.0.0 and English-only commit messages.
set -euo pipefail

msg_file="${1:?usage: commit-msg <file>}"
python3 - "$msg_file" <<'PY'
import re, sys
from pathlib import Path

raw = Path(sys.argv[1]).read_text(encoding="utf-8")
lines = [l for l in raw.splitlines() if not l.startswith("#")]
while lines and not lines[0].strip():
    lines.pop(0)
if not lines:
    sys.exit("commit-msg: empty commit message")

subject = lines[0].strip()
if subject.startswith(("Merge ", "Revert \"")):
    sys.exit(0)

conv_re = re.compile(
    r"^(feat|fix|chore|ci|docs|refactor|test|perf|build|style|revert)(\([a-z0-9_./-]+\))?!?: .+$"
)
if not conv_re.match(subject):
    sys.exit(
        f"commit-msg: invalid Conventional Commit header: {subject!r}\n"
        "Expected: <type>[optional scope]: <description in English>\n"
        "Allowed types: feat, fix, chore, ci, docs, refactor, test, perf, build, style, revert"
    )

body = "\n".join(lines)
cleaned = re.sub(r"\b(Guará|Sabiá|Acauã|Ciberfauna)\b", "", body)
pt_accents = re.search(r"[áàâãéêíóôõúçÁÀÂÃÉÊÍÓÔÕÚÇ]", cleaned)
pt_words = re.search(
    r"\b(não|para|como|quando|então|pelos|pelas|pelo|pela|estão|está|também|versão|"
    r"corrige|adiciona|atualiza|remove|padroniza|implementa|ajusta|arquivo|arquivos)\b",
    cleaned,
    re.I,
)
if pt_accents or pt_words:
    sys.exit("commit-msg: commit messages must be written 100% in English.")
PY
