"""Build the item set for the self-tuning experiment: (raw ASR without vocabulary, truth script).
zh: 38 Module 2 memos (raw = corpus/transcripts_grok.json) + 126 lines of the 3.7 meeting, each line transcribed
on its own with Grok, no terms. Usage: build_items.py <lang>   (needs AI_BUILDER_TOKEN for the meeting lines)"""
import json, os, sys, time, concurrent.futures as cf
from pathlib import Path
import httpx

LANG = sys.argv[1]
HERE = Path(__file__).resolve().parent
EXP = HERE.parent
corpus = EXP / "corpus" if LANG == "zh" else EXP.parent / "experiments_en" / "corpus"
meeting = EXP / "3_7_formulation" / f"meeting_{LANG}"
URL = "https://space.ai-builders.com/backend/v1/audio/grok-transcription"

def asr(path):
    for attempt in range(4):
        try:
            with open(path, "rb") as f:
                r = httpx.post(URL, headers={"Authorization": f"Bearer {os.environ['AI_BUILDER_TOKEN']}"},
                               files={"audio_file": (path.name, f)}, data={"language": LANG, "simple": "true"}, timeout=180)
            r.raise_for_status(); return r.json()["text"]
        except Exception as e:
            print("retry", path.name, e, flush=True); time.sleep(5 * (attempt + 1))
    raise RuntimeError(path)

items = []
memos = json.loads((corpus / "memos.json").read_text(encoding="utf-8"))["memos"]
raw_memo = json.loads((corpus / "transcripts_grok.json").read_text(encoding="utf-8"))
for m in memos:
    items.append({"id": m["id"], "source": "memo", "raw": raw_memo[m["id"]], "truth": m["text"]})

cache = HERE / f"meeting_lines_raw_{LANG}.json"
raw_lines = json.loads(cache.read_text(encoding="utf-8")) if cache.exists() else {}
script = json.loads((meeting / "script.json").read_text(encoding="utf-8"))
wavs = {int(p.name.split("_")[0]): p for p in (meeting / "lines").glob("*.wav")}
todo = [i for i, l in enumerate(script) if l["speaker"] != "BREAK" and str(i) not in raw_lines]
with cf.ThreadPoolExecutor(6) as ex:
    for i, text in zip(todo, ex.map(lambda i: asr(wavs[i]), todo)):
        raw_lines[str(i)] = text
cache.write_text(json.dumps(raw_lines, ensure_ascii=False, indent=1), encoding="utf-8")
for i, l in enumerate(script):
    if l["speaker"] != "BREAK":
        items.append({"id": f"M{i:03d}", "source": "meeting", "speaker": l["speaker"], "raw": raw_lines[str(i)], "truth": l["text"]})
(HERE / f"items_{LANG}.json").write_text(json.dumps(items, ensure_ascii=False, indent=1), encoding="utf-8")
print(len(items), "items")
