"""Check Indonesian title-style UI labels.

This guards against mixed sentence-case labels in title/button areas such as
"Halaman catatan" or "Lihat semua libur" appearing next to Title Case labels.
Run from project root: python tools/indonesian_title_case_check.py --write-report
"""
from __future__ import annotations

import argparse
import json
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
LANG_PATH = ROOT / "lang" / "id_ID.json"
REPORT_PATH = ROOT / "data" / "indonesian_title_case_latest.json"

# These are labels rendered as page titles, tab labels, action buttons, or card
# titles.  Sentence/help text is intentionally not included.
EXPECTED_LABELS = {
    "notes_section": "Halaman Catatan",
    "year_view_classic": "Tampilan Libur",
    "year_open_month": "Lihat Semua",
    "year_open_all_holidays": "Lihat Semua Libur",
    "year_overview_click_hint": "Klik Tanggal untuk Catatan, Double-Click untuk Tambah/Edit",
    "chat_scope_enterprise": "Mode Ruang Kerja Tim",
    "chat_scope_personal": "Mode Komunikasi Personal",
    "chat_personal_notes": "Catatan Pribadi",
    "chat_personal_todo": "Tugas Pribadi",
    "chat_stickers": "Paket Stiker",
    "chat_pack_work": "Stiker Kerja",
    "chat_pack_mood": "Stiker Mood",
    "chat_pack_office": "Stiker Kantor",
    "quick_custom_label": "Nama Khusus",
    "home_quick_actions_hint": "Akses Cepat Umum",
    "crm_quick_ticket": "Buat Tiket Cepat",
    "crm_customer_name": "Nama Pelanggan",
    "crm_contact_info": "Kontak Pelanggan",
    "crm_issue_title": "Judul Masalah / Kebutuhan",
    "crm_add_ticket": "Tambah Tiket",
    "crm_ticket_list": "Tiket Pelanggan Terbaru",
    "crm_stat_open": "Belum Selesai",
    "crm_mark_following": "Tandai Follow-Up",
    "crm_mark_resolved": "Tandai Selesai",
    "focus_save_today": "Simpan Ke Hari Ini",
    "quick_cmd_add_task": "Tambah Tugas Cepat",
    "quick_cmd_add_note": "Tambah Catatan Hari Ini",
    "quick_cmd_notifications": "Buka Pusat Notifikasi",
    "quick_cmd_focus_timer": "Buka Timer Fokus",
    "quick_cmd_system_health": "Buka Sistem Kesehatan",
}


def build_report() -> dict:
    payload = json.loads(LANG_PATH.read_text(encoding="utf-8"))
    problems = []
    for key, expected in EXPECTED_LABELS.items():
        actual = payload.get(key)
        if actual != expected:
            problems.append({"key": key, "expected": expected, "actual": actual})
    return {
        "checked_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "language": "id_ID",
        "checked_labels": len(EXPECTED_LABELS),
        "status": "OK" if not problems else "FAILED",
        "problems": problems,
        "notes": [
            "Only title/button/card labels are checked; normal helper sentences may keep sentence case.",
            "This prevents mixed UI text like 'Halaman catatan' beside Title Case labels.",
        ],
    }


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--write-report", action="store_true")
    args = parser.parse_args()
    report = build_report()
    if args.write_report:
        REPORT_PATH.parent.mkdir(parents=True, exist_ok=True)
        REPORT_PATH.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
    print("NexaOffice Indonesian Title Case Check")
    print(f"- Checked labels: {report['checked_labels']}")
    print(f"- Status: {report['status']}")
    if report["problems"]:
        for item in report["problems"][:20]:
            print(f"  {item['key']}: expected={item['expected']!r}, actual={item['actual']!r}")
    if args.write_report:
        print(f"- Report: {REPORT_PATH.relative_to(ROOT)}")
    return 0 if report["status"] == "OK" else 1


if __name__ == "__main__":
    raise SystemExit(main())
