#!/usr/bin/env python3
"""Update specific music disc description localization keys across all language files.

Keys updated:
  item.minecraft.music_disc_chirp.desc -> Big F - Fysik
  item.minecraft.music_disc_blocks.desc -> Yunge Kutta - Plugga
  item.minecraft.music_disc_13.desc -> Yunge Kutta - Nabla Aura
  item.minecraft.music_disc_11.desc -> Yunge Kutta - Linjären
  item.minecraft.music_disc_cat.desc -> Yunge Kutta - F (Skålar & Pokaler)

Only modifies files where at least one of the keys exists and value differs.
Preserves key ordering and formatting style (2-space indent) while ensuring a trailing newline.
"""
from __future__ import annotations

import json
from pathlib import Path

LANG_DIR = Path('assets/minecraft/lang')

MAPPING = {
    "item.minecraft.music_disc_chirp.desc": "Big F - Fysik",
    "item.minecraft.music_disc_blocks.desc": "Yunge Kutta - Plugga",
    "item.minecraft.music_disc_13.desc": "Yunge Kutta - Nabla Aura",
    "item.minecraft.music_disc_11.desc": "Yunge Kutta - Linjären",
    "item.minecraft.music_disc_cat.desc": "Yunge Kutta - F (Skålar & Pokaler)",
}

def main() -> int:
    if not LANG_DIR.is_dir():
        print(f"Language directory not found: {LANG_DIR}")
        return 1
    changed_files = 0
    for path in sorted(LANG_DIR.glob('*.json')):
        try:
            text = path.read_text(encoding='utf-8')
            data = json.loads(text)
        except Exception as e:
            print(f"Skipping {path}: cannot parse JSON ({e})")
            continue
        updated = False
        for key, value in MAPPING.items():
            if key in data and data[key] != value:
                data[key] = value
                updated = True
        if updated:
            # Dump with indent=2 and ensure_ascii False to keep UTF-8 characters
            new_text = json.dumps(data, ensure_ascii=False, indent=2, sort_keys=False) + "\n"
            # Minimize diff: only overwrite if content changed
            if new_text != text:
                path.write_text(new_text, encoding='utf-8')
                changed_files += 1
                print(f"Updated: {path}")
    print(f"Done. Files changed: {changed_files}")
    return 0

if __name__ == '__main__':  # pragma: no cover
    raise SystemExit(main())
