#!/usr/bin/env python3
"""Find all t('key') usages in templates that fall back to English for fa/tr/ar."""
import json
import os
import re
import sys

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

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

# scan templates for t('key') and t("key")
used = set()
tpl_dir = os.path.join(BASE, 'app', 'templates')
for root, _dirs, files in os.walk(tpl_dir):
    for fn in files:
        if fn.endswith('.html'):
            with open(os.path.join(root, fn), encoding='utf-8') as f:
                src = f.read()
            for m in re.finditer(r"""\bt\(\s*['"]([a-z0-9_]+)['"]""", src):
                used.add(m.group(1))

# also python blueprints
bp_dir = os.path.join(BASE, 'app', 'blueprints')
for root, _dirs, files in os.walk(bp_dir):
    for fn in files:
        if fn.endswith('.py'):
            with open(os.path.join(root, fn), encoding='utf-8') as f:
                src = f.read()
            for m in re.finditer(r"""(?:t\(|gettext\()\s*['"]([a-z0-9_]+)['"]""", src):
                used.add(m.group(1))

print(f'Total distinct keys used: {len(used)}')
for lang in ['fa', 'tr', 'ar']:
    missing = sorted(k for k in used if k not in data[lang])
    print(f'\n{lang}: {len(missing)} missing')
    for k in missing:
        print(f'  {k} = {data["en"].get(k, "?")!r}')
