import json
import sys
from pathlib import Path

# Ensure stdout handles UTF-8 correctly for console printing
if hasattr(sys.stdout, 'reconfigure'):
    sys.stdout.reconfigure(encoding='utf-8')

LOCALES_DIR = Path("src/locales")
USER_MASTER = LOCALES_DIR / "translations.user.json"
ADMIN_MASTER = LOCALES_DIR / "translations.admin.json"
LANGUAGES = ["en", "me", "ru", "ua"]

def is_cyrillic(s):
    """Checks if a string contains any Cyrillic characters."""
    return any('\u0400' <= c <= '\u04FF' for c in s)

def find_leaks(data, path=""):
    """
    Recursively searches through translation objects to find:
    1. Cyrillic characters in 'en' or 'me' fields (leaked Russian/Ukrainian).
    2. Specific Russian-only characters (ы, ъ, ё, э) in 'ua' fields.
    """
    leaks = []
    if isinstance(data, dict):
        # If this is a leaf leaf node containing language keys
        if all(lang in data for lang in LANGUAGES):
            for lang in LANGUAGES:
                val = str(data[lang])
                
                # Check for Cyrillic in languages that use Latin (en, me)
                if lang in ["en", "me"] and is_cyrillic(val):
                    leaks.append((f"{path} [{lang}]", val))
                
                # Check for Russian-only characters in Ukrainian
                if lang == "ua":
                    russian_only = ['ы', 'ъ', 'ё', 'э', 'Ы', 'Ъ', 'Ё', 'Э']
                    if any(c in val for c in russian_only):
                        leaks.append((f"{path} [ua]", val))
        else:
            # Continue deeper into the structure
            for k, v in data.items():
                leaks.extend(find_leaks(v, f"{path}.{k}" if path else k))
    return leaks

def run_check(master_path, name):
    print(f"\n--- Checking for leaks in {name} ({master_path.name}) ---")
    if not master_path.exists():
        print(f"Error: {master_path} not found.")
        return

    with open(master_path, "r", encoding="utf-8") as f:
        data = json.load(f)

    leaks = find_leaks(data)
    if not leaks:
        print("Success: No language leaks found!")
    else:
        for p, val in leaks:
            print(f"Leak found at {p}: {val}")

if __name__ == "__main__":
    run_check(USER_MASTER, "User Translations")
    run_check(ADMIN_MASTER, "Admin Translations")
