#!/usr/bin/env python3
"""draws_review.py — the film, window by window: done / rendering / failed / not started.

DERIVED, never maintained (SKILL.md: "a review page must be derived"). Built from
plan.json (+ broll-plan.json), the render queue (landed clips, parked draws) and
lane-render.log. Every verdict the driver wrote is translated into plain words.

Operator, 2026-08-22: "this makes no sense to me — clean it up so I understand what is
done, what failed and what is working." So: one row per window in film order, a status
badge, the landed clip playable under it, failed draws collapsed with a one-line reason,
and a summary at the top. The technical material (references, slices) is collapsed.

Usage: draws_review.py --project <dir> [--out build/draws-review.html]
"""
from __future__ import annotations

import argparse
import glob
import html
import json
import os
import re
import subprocess
import sys
import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from lane_nav import inject_nav  # noqa: E402
from lane_page import page as shell  # noqa: E402

DRAWS_CSS = """article{display:flex;gap:16px;align-items:flex-start;border-left:6px solid #444;background:#181818;border-radius:8px;padding:12px 14px;margin:10px 0}
article.done{border-left-color:#3fa24a} article.now{border-left-color:#e3b23c} article.retry{border-left-color:#d9772e} article.fail{border-left-color:#c0392b} article.todo{border-left-color:#555;opacity:.85}
.head{flex:1} h2{font-size:16px;margin:0 0 4px} .st{font-size:12px;padding:2px 8px;border-radius:10px;background:#333;margin-left:8px}
.done .st{background:#3fa24a;color:#fff} .now .st{background:#e3b23c;color:#111} .retry .st{background:#d9772e;color:#fff} .fail .st{background:#c0392b;color:#fff}
.what{margin:0;color:#bbb;font-size:13.5px} .ok{margin:6px 0 0;color:#9ad} details{margin-top:6px;font-size:13px;color:#e8a0a0} details a{color:#f0c36a}
.clip video{width:150px;aspect-ratio:9/16;background:#000;border-radius:6px}
.slice{font-size:12px;color:#9ad;margin-top:6px;width:220px} .slice audio{width:220px;display:block;margin-top:4px}
.fails{margin-top:8px} .rej{border-top:1px dashed #444;padding:8px 0} .rejhead{color:#e8a0a0;font-size:13px;margin-bottom:6px}
.pair{display:flex;gap:14px;align-items:flex-start} .pair video{width:170px;aspect-ratio:9/16;background:#000;border-radius:6px} .gone{color:#777;font-size:12px}
.vid{display:inline-block;text-align:center} .vid video{display:block} .clip .vid video{width:190px}
.t{font:13px/1.4 ui-monospace,Menlo,monospace;color:#ddd;margin-top:4px} .t .len{color:#9ad}
button.play{margin-top:6px;padding:8px 18px;font-size:14px;font-weight:600;border:0;border-radius:8px;background:#e8c36a;color:#111;cursor:pointer}
button.play.on{background:#3fa24a;color:#fff} button.both{background:#7fb3ff} figure{display:inline-block;margin:0 16px 0 0} figure img{height:160px;border-radius:6px}
table{font-size:12px} td{padding:2px 8px} .legend span{display:inline-block;margin-right:14px;font-size:12.5px} .legend i{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:4px}"""


# --- the driver's log lines, in plain words -------------------------------------------
def plain(verdict: str) -> str:
    if verdict.startswith("(earlier cut of this window) "):
        return "earlier cut of this window — " + plain(verdict[len("(earlier cut of this window) "):])
    v = verdict
    m = re.search(r"correlates ([\d.]+) but sits ([+-]\d+)ms", v)
    if m:
        ms = int(m.group(2)); return f"mouth {abs(ms)/1000:.1f} s {'late' if ms > 0 else 'early'} (followed the track well, score {m.group(1)})"
    m = re.search(r"WRONG TIME — correlates ([\d.]+) .* at ([+-]\d+)ms", v)
    if m:
        return f"mouth did not follow the track (score {m.group(1)}, needs 0.6)"
    m = re.search(r"sings (\d+)% of its reference", v)
    if m:
        return f"invented its own vocal (only {m.group(1)}% of the words)"
    if "sang over silence" in v:
        return "sang over a silent window (lips should have been closed)"
    if "borderline" in v and "cannot be measured" in v:
        m = re.search(r"\(([\d.]+) @ ([+-]\d+)ms\)", v)
        return f"just under the bar (score {m.group(1)} at {m.group(2)} ms) — parked" if m else "just under the bar — parked"
    if "moderation refused" in v or "nsfw" in v.lower():
        return "refused by the provider's moderation (random; redrawn)"
    if "STOPPED" in v:
        return "stopped: same failure twice — the window was re-cut"
    if "no audio stream" in v or "could not transcribe" in v:
        return "silent shot as intended (no vocal track to check)"
    m = re.search(r"on the bar \(corr ([\d.]+) @ ([+-]\d+)ms\)", v)
    if m:
        return f"in sync (score {m.group(1)}, {m.group(2)} ms)"
    m = re.search(r"BORDERLINE \(corr ([\d.]+) @ ([+-]\d+)ms", v)
    if m:
        return f"kept, just under the bar (score {m.group(1)}, {m.group(2)} ms)"
    return v[:160]


def clip_seconds(path: str) -> str:
    r = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", path],
                       capture_output=True, text=True)
    try:
        return f"{float(r.stdout.strip()):.1f} s"
    except ValueError:
        return "? s"


def vid(path: str) -> str:
    """A video player with a Play button, a live 0:00 / 0:00 readout and the probed length."""
    return (f'<div class="vid"><video controls preload="metadata" src="file://{html.escape(path)}" '
            f'ontimeupdate="tick(this)" onloadedmetadata="tick(this)"></video>'
            f'<div class="t"><span class="now">0:00</span> / <span class="len">{clip_seconds(path)}</span></div>'
            f'<button class="play" onclick="tog(this)">▶ Play</button> '
            f'<button class="play both" onclick="both(this)">▶ Both</button></div>')


def parse_log(path: str) -> tuple[dict, dict, str]:
    """(landed[id] -> line, draws[(id, n)] -> line, current 'id draw n' if rendering)."""
    landed, draws, current = {}, {}, ""
    if not os.path.isfile(path):
        return landed, draws, current
    # the log is the whole film's history: draws made before the LATEST approval were
    # rendered against an earlier cut of the windows — keep them, but say so
    lines = [l.rstrip("\n") for l in open(path, encoding="utf-8", errors="replace")]
    last_approval = max((i for i, l in enumerate(lines) if "approval " in l and "cleared" in l), default=-1)
    for idx, line in enumerate(lines):
        body = re.sub(r"^\d\d:\d\d:\d\d\s+", "", line)
        m = re.match(r"(w\d\d|b\d\d): (.*)$", body)
        if not m:
            if "=== lane_render" in body:
                current = ""
            continue
        jid, rest = m.group(1), m.group(2)
        tag = "(earlier cut of this window) " if idx < last_approval else ""
        if rest.startswith("-> "):
            landed[jid] = rest; current = ""
        elif rest.startswith("REJECTED draw") or rest.startswith("moderation refused draw"):
            n = int(re.search(r"draw (\d)", rest).group(1)); draws[(jid, n)] = tag + rest; current = ""
        elif rest.startswith("STOPPED"):
            draws[(jid, 99)] = tag + rest; current = ""
        elif rest.startswith("ACCEPTED"):
            n = re.search(r"\(draw (\d)\)", rest); current = f"{jid} draw {n.group(1) if n else '?'} (submitted {line[:8]})"
        elif "an earlier submission is unaccounted for" in rest:
            current = f"{jid} (adopted an in-flight draw, polling it — {line[:8]})"
        elif rest.startswith("redrawing") or "ACCEPTED" in rest:
            pass
    return landed, draws, current


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--project", required=True)
    ap.add_argument("--out", default="")
    args = ap.parse_args()
    P = os.path.abspath(args.project); b = os.path.join(P, "build")
    out = args.out or os.path.join(b, "draws-review.html")
    plan = json.load(open(os.path.join(b, "plan.json"), encoding="utf-8")) if os.path.isfile(os.path.join(b, "plan.json")) else {"jobs": []}
    jobs = list(plan.get("jobs", []))
    bp = os.path.join(b, "broll-plan.json")
    if os.path.isfile(bp):
        jobs += json.load(open(bp, encoding="utf-8")).get("jobs", [])
    landed, draws, current = parse_log(os.path.join(b, "lane-render.log"))
    clips = os.path.join(b, "queue", "explore", "clips"); parked = os.path.join(b, "queue", "explore", "superseded")
    bclips = os.path.join(b, "broll", "clips")
    names = {}
    pack_names = os.environ.get("PERFORMER_NAMES", "")
    if pack_names:
        p = pack_names.split("|"); names = {"a": p[0], "b": p[1] if len(p) > 1 else "", "both": " & ".join(p[:2])}
    who_of = {"a": names.get("a", "him"), "b": names.get("b", "her"), "both": names.get("both", "both")}

    rows, done, failed_now = [], 0, 0
    for j in jobs:
        jid = j["id"]; is_b = jid.startswith("b")
        clip = os.path.join(bclips if is_b else clips, f"{jid}.mp4")
        on_disk = os.path.isfile(clip)
        fails = sorted((n, v) for (i, n), v in draws.items() if i == jid and n != 99)
        stopped = (jid, 99) in draws
        reused = os.path.join(b, "queue", "explore", f"{jid}-reused.json")
        reuse_note = ""
        if on_disk and os.path.isfile(reused):
            try:
                rr = json.load(open(reused, encoding="utf-8"))
                src = rr.get("from") or ""
                reuse_note = (f"reused a clip we already had ({os.path.basename(os.path.dirname(os.path.dirname(src)))}"
                              f"/{os.path.basename(src)}) — {rr.get('why') or ''}")
            except (OSError, ValueError):
                reuse_note = "reused a clip we already had"
        if on_disk:
            status, cls = ("DONE — reused, no render needed" if reuse_note else "DONE"), "done"; done += 1
        elif j.get("deferred") and not fails and not current.startswith(jid + " "):
            status, cls = "DEFERRED — rendered only when the NEEDS report asks", "todo"
        elif current.startswith(jid + " "):
            status, cls = f"RENDERING — {current}", "now"
        elif stopped:
            status, cls = "STOPPED (re-cut)", "fail"
        elif fails:
            status, cls = f"RETRYING — {len(fails)} failed draw(s)", "retry"; failed_now += 1
        else:
            status, cls = "not started", "todo"
        v = j.get("vocal") or {}
        if is_b:
            what = "B-roll cutaway (no people) for an instrumental gap"
        elif not j.get("voiceRef"):
            what = f"{who_of.get(j.get('performer', 'a'))} — no singing, lips closed (intro)"
        else:
            lines = "; ".join(f"“{s['text']}”" for s in v.get("spans", [])[:3])
            what = f"{who_of.get(j.get('performer', 'a'))} sings {lines}"
        start = float(j.get("startSec", 0)) + float(j.get("trimStartSec", 0) or 0)
        secs = float(j.get("seconds", 0)) - float(j.get("trimStartSec", 0) or 0) - float(j.get("tailSec", 0) or 0)
        where = "" if is_b else f"{start:.0f}–{start + secs:.0f} s of the song · "
        # THE SONG FOR THIS WINDOW, ALWAYS. The voice slice only exists for synced windows;
        # the operator compares every clip (landed, rejected, silent intro) against the
        # song, so cut the window's seconds from the master into build/audio-preview/.
        seg = os.path.join(b, "audio-preview", f"{jid}.m4a")
        hook = os.path.join(b, "hook.wav")
        if not is_b and os.path.isfile(hook) and (not os.path.isfile(seg) or os.path.getmtime(seg) < os.path.getmtime(hook)):
            os.makedirs(os.path.dirname(seg), exist_ok=True)
            subprocess.run(["ffmpeg", "-v", "error", "-y", "-ss", f"{start:.3f}", "-t", f"{secs:.3f}", "-i", hook,
                            "-c:a", "aac", "-b:a", "128k", seg], check=False)
        slice_audio = (f'<div class="slice"><b>the song here</b> ({start:.0f}–{start + secs:.0f} s)'
                       f'<audio controls preload="none" src="file://{html.escape(seg)}"></audio></div>'
                       if os.path.isfile(seg) else "")
        player = (vid(clip) if on_disk else "") + slice_audio
        verdict = plain(landed.get(jid, "")) if on_disk and landed.get(jid) else ""
        if reuse_note:
            verdict = (verdict + " · " if verdict else "") + reuse_note
        # SUGGESTIONS for windows still to render: the inventory pass records the
        # nearest clip that was NOT placed (face unknown, or just outside the bar) so
        # the operator can accept one by eye instead of waiting on the lane.
        if not on_disk and not is_b:
            rep = os.path.join(b, "reuse-report.json")
            if os.path.isfile(rep):
                try:
                    nm = (json.load(open(rep, encoding="utf-8")).get("nearMisses") or {}).get(jid)
                except (OSError, ValueError):
                    nm = None
                if nm:
                    face = {True: "right face", False: "WRONG face", None: "face unknown"}[nm.get("faceOk")]
                    verdict = (f"could reuse {os.path.basename(nm['clip'])}: matches {nm['corr']:.2f} "
                               f"@ {nm['offsetMs']:+d} ms, {face} — not placed automatically; "
                               f"REUSE_FORCE=\"{jid}={nm['clip']}\" accepts it by eye")
        # REJECTED DRAWS STAY VISIBLE, each with its own player, so the operator can play
        # the take and the slice it was asked to follow and hear the difference himself.
        fail_rows = ""
        for n, vv in fails:
            pf = os.path.join(parked, f"{jid}-draw{n}-ignored-voice.mp4")
            if not os.path.isfile(pf):
                for cand in sorted(glob.glob(os.path.join(b, "queue-archive", "*", f"{jid}-draw{n}-ignored-voice.mp4"))
                                   + glob.glob(os.path.join(b, "queue-archive", "*", "superseded", f"{jid}-draw{n}-ignored-voice.mp4"))):
                    pf = cand
            pv = vid(pf) if os.path.isfile(pf) else '<span class="gone">clip not kept</span>'
            fail_rows += (f'<div class="rej"><div class="rejhead">draw {n} — REJECTED: {html.escape(plain(vv))}</div>'
                          f'<div class="pair">{pv}{slice_audio}</div></div>')
        fails_html = f'<div class="fails">{fail_rows}</div>' if fails else ""
        rows.append(f'''<article class="{cls}">
  <div class="head"><h2>{jid} <span class="st">{html.escape(status)}</span></h2>
  <p class="what">{html.escape(where)}{html.escape(what)} · {html.escape(str(j.get('styleId') or ''))}</p>
  {f'<p class="ok">{html.escape(verdict)}</p>' if verdict else ''}{fails_html}</div>
  <div class="clip">{player}</div>
</article>''')

    total = len(jobs); windows = [j for j in jobs if not j["id"].startswith("b")]
    done_w = sum(1 for j in windows if os.path.isfile(os.path.join(clips, f"{j['id']}.mp4")))
    remaining = total - done
    eta = f"~{remaining * 0.55:.0f}–{remaining * 0.8:.0f} h at ~30 min a render with retries" if remaining else "all rendered"
    summary = (f"<b>{done_w} of {len(windows)} windows done</b> · {done} of {total} jobs (incl. B-roll) · "
               f"{'rendering now: ' + html.escape(current) if current else 'driver idle'} · remaining {remaining} → {eta} · "
               f"updated {time.strftime('%H:%M')}")
    refs = []
    for j in plan.get("jobs", []):
        for k in ("identityRef", "identityRef2"):
            r = j.get(k)
            if r and r not in refs:
                refs.append(r)
    figs = "".join(f'<figure><img src="file://{html.escape(r)}"><figcaption>{html.escape(os.path.basename(r))}</figcaption></figure>' for r in refs)
    song = glob.glob(os.path.join(P, "artifacts", "audio", "*.mp3"))
    slices = "".join(f'<tr><td>{j["id"]}</td><td><audio controls preload="none" src="file://{html.escape(j["voiceRef"])}"></audio></td></tr>'
                     for j in plan.get("jobs", []) if j.get("voiceRef"))
    body = f'''<script>function fmt(t){{t=Math.floor(t||0);return Math.floor(t/60)+':'+String(t%60).padStart(2,'0');}}
function tick(v){{var d=v.parentNode.querySelector('.now');if(d)d.textContent=fmt(v.currentTime);}}
function both(b){{var v=b.parentNode.querySelector('video');var box=b.closest('.clip,.pair');var a=box?box.querySelector('audio'):null;
 if(v.paused){{v.currentTime=0;if(a){{a.currentTime=0;a.play();}}v.play();b.textContent='❚❚ Both';b.classList.add('on');
  v.onended=function(){{if(a)a.pause();b.textContent='▶ Both';b.classList.remove('on');}};}}
 else{{v.pause();if(a)a.pause();b.textContent='▶ Both';b.classList.remove('on');}}}}
function tog(b){{var v=b.parentNode.querySelector('video');if(v.paused){{v.play();b.textContent='❚❚ Pause';b.classList.add('on');}}else{{v.pause();b.textContent='▶ Play';b.classList.remove('on');}}v.onended=function(){{b.textContent='▶ Play';b.classList.remove('on');}};}}</script>
<p class="sum">{summary}</p>
<p class="legend"><span><i style="background:#3fa24a"></i>done: clip landed and in sync</span><span><i style="background:#e3b23c"></i>rendering now</span><span><i style="background:#d9772e"></i>failed draws so far, retrying</span><span><i style="background:#c0392b"></i>stopped</span><span><i style="background:#555"></i>not started</span></p>
{"".join(rows)}
<details style="margin-top:22px;color:#bbb"><summary>References sent with every render (faces, voice slices)</summary>{figs}
{"".join(f'<p>Main song: <audio controls preload="none" src="file://{html.escape(s)}"></audio></p>' for s in song)}
<table>{slices}</table></details>'''
    doc = shell("Film status — window by window",
                "Every window: landed and in sync, rendering now, failed, or still to come — with the take, its slice and the reasons.",
                body, extra_css=DRAWS_CSS)
    open(out, "w", encoding="utf-8").write(doc)
    inject_nav(out)
    print(f"draws_review: {done_w}/{len(windows)} windows done -> {out}", file=sys.stderr)


if __name__ == "__main__":
    main()
