#!/usr/bin/env python3
"""Generate bench/qa_questions_<lang>.txt from the English set via
opus-mt-en-<lang> (#000056 §9 multi-bread). usage: make_lang_questions.py <lang>"""
from __future__ import annotations
import json, sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from arborist.qa.mt import OpusMTTranslator

LANG = sys.argv[1] if len(sys.argv) > 1 else "es"
SRC = ROOT / "bench" / "qa_questions.txt"
OUT = ROOT / "bench" / f"qa_questions_{LANG}.txt"
MAP = ROOT / "bench" / f"qa_questions_{LANG}_map.json"

def main() -> int:
    tr = OpusMTTranslator()
    out = [f"# AUTO-GENERATED from qa_questions.txt via opus-mt-en-{LANG} (#000056).",
           "# Regenerate: python3 bench/make_lang_questions.py " + LANG, ""]
    pairs = []
    n = 0
    for ln in SRC.read_text().splitlines():
        s = ln.strip()
        if not s or s.startswith("#"):
            out.append(ln); continue
        t = tr.translate(s, "en", LANG)
        if not tr.available:
            print("opus-mt unavailable", file=sys.stderr); return 1
        out.append(t); pairs.append({"en": s, LANG: t}); n += 1
        print(f"  [{n}] {s}  ->  {t}", flush=True)
    OUT.write_text("\n".join(out) + "\n")
    MAP.write_text(json.dumps(pairs, ensure_ascii=False, indent=2) + "\n")
    print(f"wrote {OUT} ({n})  + {MAP}", flush=True)
    return 0

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