"""Build and run WS4 arm calls.

usage: python run_arms.py <dset> <slug,slug|ALL|FIRST:k|FROM:k> [--arms a,b,c] [--cap USD] [--dry]
Jobs -> raw/jobs.jsonl (one row per dset/slug/arm/call_id/rep, idempotent).
Calls -> raw/calls.jsonl (one row per prompt_sha/rep; identical prompts across dsets are called once).
"""
from __future__ import annotations

import json
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

import ws4lib as L

RAW = L.WS4 / "raw"
JOBS = RAW / "jobs.jsonl"
CALLS = RAW / "calls.jsonl"
REPS = (1, 2)
lock = threading.Lock()


def read_jsonl(p):
    if not p.exists():
        return []
    return [json.loads(x) for x in p.read_text(encoding="utf-8").splitlines() if x.strip()]


def build_jobs(dset, slugs, arms):
    have = {(j["dset"], j["slug"], j["arm"], j["call_id"], j["rep"]) for j in read_jsonl(JOBS)}
    new = []
    for slug in slugs:
        meta, rubric, evidence = L.load_bundle(dset, slug)
        text = L.load_draft(dset, slug)
        units, b_texts, _ = L.classify_units(dset, slug, text)
        calls = []
        if "a" in arms:
            calls.append(("a", "whole", text, [u["pos"] for u in units]))
        if "b" in arms:
            for b in b_texts:
                calls.append(("b", f"sec{b['sec_idx']}", b["b_text"],
                              [u["pos"] for u in units if u["sec_idx"] == b["sec_idx"]]))
        if "c" in arms:
            groups = {}
            for u in units:
                groups.setdefault(u["c_group"], (u["c_text"], []))[1].append(u["pos"])
            for g, (t, poss) in groups.items():
                calls.append(("c", g, t, poss))
        tmpl_sha = L.sha(L.build_user(meta, rubric, evidence, "<<DOSSIER>>"))
        for arm, cid, dtext, poss in calls:
            psha = L.sha(L.SYS + "\n\x00\n" + L.build_user(meta, rubric, evidence, dtext))
            for rep in REPS:
                if (dset, slug, arm, cid, rep) in have:
                    continue
                new.append({"dset": dset, "slug": slug, "arm": arm, "call_id": cid, "rep": rep,
                            "unit_pos": poss, "dossier_text": dtext, "dossier_sha": L.sha(dtext),
                            "prompt_sha": psha, "template_sha": tmpl_sha})
    with JOBS.open("a", encoding="utf-8") as f:
        for j in new:
            f.write(json.dumps(j, ensure_ascii=False) + "\n")
    return new


def spent():
    return sum((c.get("cost") or 0) for c in read_jsonl(CALLS))


def run(dset, slugs, arms, cap, dry=False):
    build_jobs(dset, slugs, arms)
    jobs = [j for j in read_jsonl(JOBS) if j["dset"] == dset and j["slug"] in slugs and j["arm"] in arms]
    done = {(c["prompt_sha"], c["rep"]) for c in read_jsonl(CALLS) if c.get("ok")}
    todo, seen = [], set()
    for j in jobs:
        k = (j["prompt_sha"], j["rep"])
        if k in done or k in seen:
            continue
        seen.add(k)
        todo.append(j)
    print(f"[{dset}] jobs {len(jobs)}  unique calls to make {len(todo)}  spent so far ${spent():.4f}", flush=True)
    if dry or not todo:
        return
    total = [spent()]
    bundles = {}

    def work(j):
        if total[0] >= cap:
            return None
        key = (j["dset"], j["slug"])
        if key not in bundles:
            bundles[key] = L.load_bundle(*key)
        meta, rubric, evidence = bundles[key]
        user = L.build_user(meta, rubric, evidence, j["dossier_text"])
        r = L.call_model(L.SYS, user)
        row = {"prompt_sha": j["prompt_sha"], "rep": j["rep"], "dset": j["dset"], "slug": j["slug"],
               "arm": j["arm"], "call_id": j["call_id"], "ts": time.strftime("%Y-%m-%dT%H:%M:%S"),
               **{k: r.get(k) for k in ("ok", "provider", "served_model", "prompt_tokens",
                                        "completion_tokens", "cost", "latency_s", "total_s", "attempts",
                                        "errors", "finish", "max_tokens")}}
        if r.get("ok"):
            j_, verdict, parsed = L.parse_answer(r["content"])
            row.update({"parsed": parsed, "verdict": verdict, "answer": j_,
                        "raw": None if parsed else r["content"][:3000]})
        with lock:
            with CALLS.open("a", encoding="utf-8") as f:
                f.write(json.dumps(row, ensure_ascii=False) + "\n")
            total[0] += row.get("cost") or 0
        return row

    n = 0
    t0 = time.time()
    with ThreadPoolExecutor(max_workers=3) as ex:
        futs = [ex.submit(work, j) for j in todo]
        for f in as_completed(futs):
            try:
                r = f.result()
            except L.Stop as e:
                print("STOP:", e, flush=True)
                ex.shutdown(wait=False, cancel_futures=True)
                break
            n += 1
            if n % 50 == 0:
                print(f"  {n}/{len(todo)} spent ${total[0]:.4f} {time.time()-t0:.0f}s", flush=True)
    print(f"[{dset}] done {n} calls; cumulative spend ${spent():.4f}", flush=True)


def parse_slugs(dset, arg):
    allv = L.seeded_order(L.slugs_for(dset))
    if arg == "ALL":
        return allv
    if arg.startswith("FIRST:"):
        return allv[: int(arg[6:])]
    if arg.startswith("FROM:"):
        return allv[int(arg[5:]):]
    return arg.split(",")


if __name__ == "__main__":
    dset, sl = sys.argv[1], sys.argv[2]
    arms = "a,b,c"
    cap = 5.0
    for i, a in enumerate(sys.argv):
        if a == "--arms":
            arms = sys.argv[i + 1]
        if a == "--cap":
            cap = float(sys.argv[i + 1])
    slugs = parse_slugs(dset, sl)
    print("vendors:", slugs, flush=True)
    run(dset, slugs, arms.split(","), cap, dry="--dry" in sys.argv)
