import json
from pathlib import Path

root = Path('.')
ref_path = root / 'en_us.json'
if not ref_path.exists():
    print('Reference en_us.json not found')
    raise SystemExit(1)

with ref_path.open('r', encoding='utf-8') as f:
    ref = json.load(f)
ref_keys = set(ref.keys())

changed = []
for p in sorted(root.glob('*.json')):
    if p.name == 'en_us.json':
        continue
    try:
        with p.open('r', encoding='utf-8') as f:
            data = json.load(f)
    except Exception as e:
        print(f'Failed to load {p}: {e}')
        continue
    if not isinstance(data, dict):
        print(f'Skipping non-dict JSON: {p}')
        continue
    new = {k: data[k] for k in data.keys() if k in ref_keys}
    if new != data:
        with p.open('w', encoding='utf-8') as f:
            json.dump(new, f, ensure_ascii=False, indent=2)
            f.write('\n')
        changed.append(p.name)
        print(f'Pruned {p.name}: {len(data)-len(new)} keys removed')
    else:
        print(f'Unchanged {p.name}')

print('\nSummary:')
print(f'Total files processed: {len(list(root.glob("*.json")))-1}')
print(f'Files changed: {len(changed)}')
for name in changed:
    print('-', name)
