import re
import sys

def main():
    with open('client/webapp/js/data/chat_i18n_catalog.js', 'r', encoding='utf-8') as f:
        text = f.read()

    langs = ['vi', 'en', 'zh', 'ja', 'ko', 'th', 'de', 'ru', 'es']
    catalog_dict = {}

    for l in langs:
        pattern = r'^\s*' + l + r':\s*\{(.*?)\}(?=,\s*(?:[a-z]{2}:|$))'
        m = re.search(pattern, text, re.DOTALL | re.MULTILINE)
        if not m:
            print(f'Could not find lang block for {l}')
            sys.exit(1)
        block = m.group(1)
        keys = re.findall(r'"([a-zA-Z0-9_-]+)"\s*:', block)
        catalog_dict[l] = keys
        print(f'Lang {l}: {len(keys)} keys')

    base_keys = catalog_dict['vi']
    base_set = set(base_keys)
    print(f'Total unique keys in vi: {len(base_set)}')

    all_ok = True
    for l in langs:
        s = set(catalog_dict[l])
        missing = base_set - s
        extra = s - base_set
        if missing:
            print(f'ERROR: {l} is missing keys: {missing}')
            all_ok = False
        if extra:
            print(f'ERROR: {l} has extra keys: {extra}')
            all_ok = False
        if len(catalog_dict[l]) != len(base_keys):
            print(f'ERROR: {l} has key count mismatch: {len(catalog_dict[l])} vs {len(base_keys)}')
            all_ok = False

    # Check for empty or non-string values
    for l in langs:
        pattern = r'^\s*' + l + r':\s*\{(.*?)\}(?=,\s*(?:[a-z]{2}:|$))'
        m = re.search(pattern, text, re.DOTALL | re.MULTILINE)
        block = m.group(1)
        pairs = re.findall(r'"([a-zA-Z0-9_-]+)"\s*:\s*"([^"]*)"', block)
        for k, v in pairs:
            if not v.strip():
                print(f'WARNING: {l} key {k} has empty value')
                all_ok = False

    # Check CHANNEL_I18N
    print('\n--- Checking CHANNEL_I18N ---')
    ch_match = re.search(r'export const CHANNEL_I18N = \{(.*?)\};', text, re.DOTALL)
    if not ch_match:
        print('ERROR: CHANNEL_I18N not found!')
        sys.exit(1)
    ch_text = ch_match.group(1)
    expected_channels = {'world', 'zone', 'guild', 'party', 'whisper', 'system', 'recruit', 'feedback'}
    for l in langs:
        ch_m = re.search(r'^\s*' + l + r':\s*\{(.*?)\}', ch_text, re.MULTILINE)
        if not ch_m:
            print(f'ERROR: CHANNEL_I18N missing {l}')
            all_ok = False
        else:
            ch_pairs = re.findall(r'"([a-zA-Z0-9_-]+)"\s*:\s*"([^"]*)"', ch_m.group(1))
            ch_keys = set(k for k, v in ch_pairs)
            if ch_keys != expected_channels:
                print(f'ERROR: CHANNEL_I18N {l} keys mismatch: {ch_keys} vs {expected_channels}')
                all_ok = False
            for k, v in ch_pairs:
                if not v.strip():
                    print(f'WARNING: CHANNEL_I18N {l} key {k} has empty value')
                    all_ok = False

    if all_ok:
        print('\nSUCCESS: 100% PARITY ACROSS ALL 9 LANGUAGES AND CHANNEL_I18N!')
    else:
        print('\nFAILED PARITY CHECKS!')
        sys.exit(1)

if __name__ == '__main__':
    main()
