#!/usr/bin/env python3
"""Check which characters needed by translations are missing from bundled fonts."""
import json
import os
from fontTools.ttLib import TTFont

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FONTS_DIR = os.path.join(ROOT, 'res', 'fonts')
LANG_DIR = os.path.join(ROOT, 'res', 'lang')

# Load font cmaps
cjk = TTFont(os.path.join(FONTS_DIR, 'NotoSansCJK-Subset.ttf'))
cjk_cmap = set(cjk.getBestCmap().keys())

ubuntu = TTFont(os.path.join(FONTS_DIR, 'Ubuntu-R.ttf'))
ubuntu_cmap = set(ubuntu.getBestCmap().keys())

combined = cjk_cmap | ubuntu_cmap

print(f"CJK subset font glyphs: {len(cjk_cmap)}")
print(f"Ubuntu font glyphs: {len(ubuntu_cmap)}")
print(f"Combined: {len(combined)}")
print()

for lang in ['zh', 'ja', 'ko', 'ru', 'de', 'es', 'fr', 'pt']:
    path = os.path.join(LANG_DIR, f'{lang}.json')
    if not os.path.exists(path):
        continue
    with open(path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    needed = set()
    for v in data.values():
        if isinstance(v, str):
            for c in v:
                needed.add(ord(c))
    
    missing = sorted(needed - combined)
    if missing:
        print(f"{lang}.json: {len(needed)} chars needed, {len(missing)} MISSING")
        for cp in missing[:20]:
            c = chr(cp)
            print(f"  U+{cp:04X} {c}")
        if len(missing) > 20:
            print(f"  ... and {len(missing) - 20} more")
    else:
        print(f"{lang}.json: OK ({len(needed)} chars, all covered)")
