#!/usr/bin/env python3
"""Verify every t('key') used in templates exists in all 4 locale files."""
import json, os, re, sys

BASE = '/home/z/my-project/fightskill'
TPL = os.path.join(BASE, 'app', 'templates')
LOC = os.path.join(BASE, 'app', 'i18n', 'locales')

locales = {}
for lang in ('en', 'fa', 'tr', 'ar'):
    with open(os.path.join(LOC, f'{lang}.json'), encoding='utf-8') as f:
        locales[lang] = json.load(f)

pat = re.compile(r"(?<![A-Za-z0-9_])t\('([a-z][a-z0-9_]*)'\)")
used = {}
for root, _dirs, files in os.walk(TPL):
    for fn in files:
        if not fn.endswith('.html'):
            continue
        p = os.path.join(root, fn)
        with open(p, encoding='utf-8') as f:
            txt = f.read()
        for m in pat.finditer(txt):
            used.setdefault(m.group(1), set()).add(os.path.relpath(p, TPL))

missing = []
for k, files in sorted(used.items()):
    for lang in ('en', 'fa', 'tr', 'ar'):
        if k not in locales[lang]:
            missing.append((k, lang, sorted(files)[:2]))

if missing:
    print(f"MISSING {len(missing)}:")
    for k, lang, files in missing:
        print(f"  {k} [{lang}]  e.g. {files}")
else:
    print(f"ALL OK — {len(used)} distinct t() keys present in all 4 locales")

# report keys used with | default(...) only (fallback safe) vs bare
print(f"\nTotal distinct keys: {len(used)}")
sys.exit(1 if missing else 0)
