#!/usr/bin/env python3
"""Generate the Spanish bench set from the English one via opus-mt-en-es.

Reproducible round-trip fixture for #000056: the SAME engine the
sandwich uses on its edges generates the Spanish questions, so the
bench measures `es(question) → [sandwich es→en] → English grounding`
end-to-end with no hand-translation, no Hermes-for-translation, no
egress. Comment / blank lines are preserved verbatim so qa_sweep.py
skips them exactly as in the English file.

Writes:
  bench/qa_questions_es.txt          one es question per line
  bench/qa_questions_es_map.json     [{en, es}, ...] in file order
"""
from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from arborist.qa.mt import OpusMTTranslator  # noqa: E402

SRC = ROOT / "bench" / "qa_questions.txt"
OUT = ROOT / "bench" / "qa_questions_es.txt"
MAP = ROOT / "bench" / "qa_questions_es_map.json"


def main() -> int:
    tr = OpusMTTranslator()
    lines = SRC.read_text().splitlines()
    out_lines: list[str] = [
        "# AUTO-GENERATED from bench/qa_questions.txt via "
        "Helsinki-NLP/opus-mt-en-es (#000056 Operation Sandwich).",
        "# Do not hand-edit — regenerate: python3 bench/make_es_questions.py",
        "",
    ]
    pairs: list[dict] = []
    n = 0
    for ln in lines:
        s = ln.strip()
        if not s or s.startswith("#"):
            out_lines.append(ln)
            continue
        es = tr.translate(s, "en", "es")
        if not tr.available:
            print("opus-mt unavailable — install 'arborist[mt]'", file=sys.stderr)
            return 1
        out_lines.append(es)
        pairs.append({"en": s, "es": es})
        n += 1
        print(f"  [{n}] {s}  ->  {es}", flush=True)
    OUT.write_text("\n".join(out_lines) + "\n")
    MAP.write_text(json.dumps(pairs, ensure_ascii=False, indent=2) + "\n")
    print(f"\nwrote {OUT}  ({n} questions)\nwrote {MAP}", flush=True)
    return 0


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