"""Score every system on every clip -> ../per-clip.json, ../summary.json.
WER: English = Whisper EnglishTextNormalizer on both sides + jiwer (same as apify-transcribe/tests/evalkit.py);
non-English = Whisper BasicTextNormalizer (lowercase, punctuation and marks removed) + jiwer.
Run with the apify-transcribe venv python (-I)."""
import json, math, pathlib, statistics as st
import jiwer
from whisper_normalizer.english import EnglishTextNormalizer
from whisper_normalizer.basic import BasicTextNormalizer

OUT = pathlib.Path(__file__).resolve().parents[1]
INP = {i["id"]: i for i in json.loads((OUT / "inputs.json").read_text())}
EN, BASIC = EnglishTextNormalizer(), BasicTextNormalizer()
GROQ_PRICE_H = {"whisper-large-v3-turbo": 0.04, "whisper-large-v3": 0.111}


def norm(t, lang):
    t = (t or "").replace("’", "'").replace("‘", "'")
    return " ".join((EN(t) if lang == "en" else BASIC(t)).split())


def wer(ref, hyp, lang):
    r, h = norm(ref, lang), norm(hyp, lang)
    if not h:
        n = len(r.split()); return {"wer": 1.0, "sub": 0, "del": n, "ins": 0, "ref_words": n}
    o = jiwer.process_words(r, h)
    return {"wer": round(o.wer, 4), "sub": o.substitutions, "del": o.deletions, "ins": o.insertions, "ref_words": len(r.split())}


def seg_text(segs):
    return " ".join((s.get("text") or "").strip() for s in segs or [])


# ---------- per-system extraction: returns {clip_id: row} ----------
def groq(model):
    rows = {}
    for p in (OUT / "runs/groq" / model).glob("*.json"):
        d = json.loads(p.read_text()); r = d["response"]
        segs = r.get("segments") or [] if isinstance(r, dict) else []
        rows[d["id"]] = {"ok": d["ok"], "status": "ok" if d["ok"] else f"HTTP {d['http']}: {(r.get('error') or {}).get('message')}",
                         "text": r.get("text", "") if d["ok"] else "", "language": r.get("language"),
                         "segments": len(segs), "has_timestamps": bool(segs and "start" in segs[0]),
                         "usd": d["list_cost_usd"], "proc_s": d["wall_s"], "audio_s": d["audio_s"]}
    return rows


def apify(name, parse):
    p = OUT / "runs/apify" / f"{name}.json"
    if not p.exists():
        return {}, None
    d = json.loads(p.read_text())
    rows = {}
    for it in d["items"]:
        cid, row = parse(it, d["url_to_id"])
        if cid:
            rows[cid] = row
    run = d["run"]
    meta = {"run_id": run["id"], "status": run["status"], "usageTotalUsd": run.get("usageTotalUsd"),
            "chargedEventCounts": run.get("chargedEventCounts"), "runTimeSecs": (run.get("stats") or {}).get("runTimeSecs"),
            "memoryMbytes": (run.get("options") or {}).get("memoryMbytes"), "buildNumber": run.get("buildNumber"),
            "client_wall_s": d["client_wall_s"], "cap_usd": d["cap_usd"], "statusMessage": run.get("statusMessage"),
            "clips": d["clip_ids"]}
    return rows, meta


def find_url(it, u2i, keys=("url", "mediaUrl", "sourceUrl", "inputUrl", "input_url", "originalUrl", "source", "input")):
    for k in keys:
        v = it.get(k)
        if isinstance(v, dict):
            v = v.get("url")
        if isinstance(v, str) and v in u2i:
            return u2i[v]
    blob = json.dumps(it)
    for u, i in u2i.items():
        if u.split("?")[0] in blob:
            return i
    return None


def generic(it, u2i):
    if it.get("status") == "run_summary":
        return None, None
    cid = find_url(it, u2i)
    segs = it.get("segments") or []
    text = it.get("text") or it.get("transcript") or it.get("fullText") or seg_text(segs)
    if isinstance(text, dict):
        text = text.get("text", "")
    status = it.get("status") or ({True: "ok", False: "failed"}.get(it.get("success")) if "success" in it else None) \
        or ("ok" if text else "no text")
    err = it.get("error") or it.get("errorMessage") or it.get("reason") or it.get("message") or it.get("statusReason") or it.get("note")
    ok = bool((text or "").strip()) and str(status).lower() in ("ok", "success", "succeeded", "done", "completed", "transcribed")
    return cid, {"ok": ok, "status": f"{status}" + (f": {err}" if err and not ok else ""), "text": text or "",
                 "language": it.get("language") or it.get("detectedLanguage") or it.get("languageCode"),
                 "segments": len(segs), "has_timestamps": bool(segs and isinstance(segs[0], dict) and "start" in segs[0]),
                 "audio_s": it.get("durationSeconds") or it.get("duration") or it.get("audioDurationSeconds") or it.get("mediaDurationSeconds"),
                 "proc_s": it.get("processingSeconds") or it.get("transcriptionSeconds"),
                 "charged_units": it.get("chargedUnits") or it.get("billedMinutes") or it.get("chargedMinutes") or it.get("billedSeconds"),
                 "raw_keys": sorted(it.keys())}


systems = {"groq turbo": (groq("whisper-large-v3-turbo"), {"price": "$0.04/h list, 10 s minimum per request"}),
           "groq large-v3": (groq("whisper-large-v3"), {"price": "$0.111/h list, 10 s minimum per request"})}
for name in ["ours_small", "steadyfetch", "tidytools", "memo23_small"]:
    rows, meta = apify(name, generic)
    if meta:
        systems[name] = (rows, meta)

fr, fmeta = apify("ours_fails", generic)
if fr and "ours_small" in systems:
    for k, v in fr.items():
        v["status"] += f" (separate run {fmeta['run_id']}, $0 charged)"
        systems["ours_small"][0][k] = v
    systems["ours_small"][1]["fails_run"] = fmeta

per = {}
for sname, (rows, meta) in systems.items():
    for cid, row in rows.items():
        if cid in INP:
            row.update(wer(INP[cid]["reference"], row["text"], INP[cid]["language"]) if row["ok"] else {"wer": None})
        row.pop("text", None)
        per.setdefault(cid, {})[sname] = row
(OUT / "per-clip.json").write_text(json.dumps(per, indent=1, ensure_ascii=False))


def pooled(sname, ids):
    e = n = 0
    for c in ids:
        r = per.get(c, {}).get(sname)
        if not r or "ref_words" not in r:
            if r is not None and c in INP:  # failed on a real clip: count as all deleted
                n += INP[c]["reference_words"]; e += INP[c]["reference_words"]
            continue
        e += r["sub"] + r["del"] + r["ins"]; n += r["ref_words"]
    return round(e / n, 4) if n else None


cats = {}
for c, i in INP.items():
    cats.setdefault(i["category"], []).append(c)
summary = {}
for sname, (rows, meta) in systems.items():
    ids = [c for c in rows if c in INP]
    s = {"meta": meta, "clips_run": len(ids), "ok": sum(rows[c]["ok"] for c in ids),
         "pooled_wer_all_run": pooled(sname, ids),
         "by_category": {k: {"n": len([c for c in v if c in rows]), "pooled_wer": pooled(sname, [c for c in v if c in rows])}
                         for k, v in cats.items()},
         "failures": {c: rows[c]["status"] for c in rows if c.startswith("fail_")},
         "audio_min_transcribed": round(sum(INP[c]["seconds"] for c in ids if rows[c]["ok"]) / 60, 3),
         "languages": {c: rows[c].get("language") for c in ids if INP[c]["category"] == "non-English"}}
    summary[sname] = s
(OUT / "summary.json").write_text(json.dumps(summary, indent=1, ensure_ascii=False))
for k, v in summary.items():
    print(k, v["clips_run"], v["ok"], v["pooled_wer_all_run"], {c: x["pooled_wer"] for c, x in v["by_category"].items()})
