#!/usr/bin/env python3
"""reuse_audit.py — every owned clip against every pending or explicitly selected repair window: what can be reused, how.

    python3 skills/rap-avatar-mv/scripts/reuse_audit.py --project <dir> \
        --index build/reuse-index.json --transcripts build/reuse-transcripts.json \
        [--out build/reuse-review.html] [--json build/reuse-review.json]

The library's fit table (index_library.py) hears only ENVELOPES; it cannot tell a take of
the right line from a take of another line with the same rhythm, and it never says WHY a
near miss missed. This audit adds the words (transcribe_clips.py on each clip's own audio)
and judges every candidate the way the operator would, per window:

  SUNG window   the candidate must carry the window's WORDS (≥ 70% of the line's words,
                in order); then the timing: on the bar (corr ≥ 0.6, |offset| ≤ 350 ms) →
                PLACE; within the slide the cut stage can absorb (≤ 400 ms) → PLACE (slide);
                words right but the offset larger → RE-CUT PROPOSAL (the window re-drawn
                around the clip: start = the record's first word − the clip's first word),
                for the operator's eye; fewer lines than the window → PARTIAL (which lines).
  DANCE window  no voice: the record plays over it, so the picture only has to be the same
                WORLD and long enough; a clip that sings words is refused (a mouth on other
                words). World unknown (an operator download) → "check by eye".

Writes one page: a summary (pending windows, placeable now, with a re-cut, uncoverable,
candidate counts) and, per pending window, its lines + keyframe and every candidate's
filmstrip, transcript and review proposal. This report never applies edits or submits jobs.
Operator, 2026-09-06: "go through all videos and see what we are going to reuse".
"""
from __future__ import annotations

import argparse
import html
import json
import os
import re
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from lane_page import page  # noqa: E402
from vocal_brief import norm_latin, transliterate  # noqa: E402

BAR_CORR, BAR_OFF_MS, SLIDE_MS = 0.6, 350, 400
WORD_MATCH_MIN = 0.7
MAX_SILENT_WORDS = 2

CSS = """
.sum{display:flex;gap:12px;flex-wrap:wrap;margin:12px 0 20px}
.sum .box{background:var(--panel,#161a22);border:1px solid var(--line,#2a2f3a);border-radius:10px;padding:10px 16px;min-width:120px}
.sum .n{font-size:26px;font-weight:700}.sum .l{opacity:.7;font-size:12px}
.win{border:1px solid var(--line,#2a2f3a);border-radius:12px;margin:18px 0;padding:12px;background:var(--panel,#161a22)}
.win h2{margin:0 0 6px;font-size:18px}.win .lines{opacity:.85;margin:4px 0 10px}
.cands{display:grid;grid-template-columns:repeat(auto-fill,minmax(360px,1fr));gap:12px}
.cand{border:1px solid #2a2f3a;border-radius:10px;overflow:hidden;background:#0f1218}
.cand img{display:block;width:100%;background:#000}.cand .m{padding:8px 10px;font-size:12.5px}
.v{display:inline-block;padding:2px 8px;border-radius:999px;font-size:11px;font-weight:700}
.v-place{background:#1f6b3a;color:#dff7e6}.v-slide{background:#2c6b1f;color:#e6f7df}.v-recut{background:#8a6d00;color:#fff5cc}
.v-partial{background:#6b4a1f;color:#ffe9c7}.v-eye{background:#33507a;color:#dbe8ff}.v-no{background:#4a2020;color:#f1c9c9}
.cmd{font-family:ui-monospace,monospace;font-size:11px;background:#000;padding:6px 8px;border-radius:6px;overflow-x:auto;white-space:pre}
.key{float:right;width:220px;margin-left:12px;border-radius:8px}
.tx{opacity:.8;font-style:italic}
"""


def words_of(text: str) -> list[str]:
    out = []
    for w in re.findall(r"[^\s,.!?…\"“”]+", text or ""):
        lat = transliterate(w) if re.search(r"[ऀ-ॿ]", w) else w
        n = norm_latin(lat).strip()
        if n:
            out.append(n)
    return out


def match_fraction(line_words: list[str], clip_words: list[str]) -> float:
    """How much of the line the clip carries: the best similarity of the line's letters
    against any same-length stretch of the clip's letters (spaces dropped, so whisper's
    "nimboo da" still meets the sheet's "nimbooda"; a clip that carries MORE lines than
    this one is not penalised for them)."""
    import difflib
    a = "".join(line_words)
    b = "".join(clip_words)
    if not a or not b:
        return 0.0
    if len(b) <= len(a):
        return difflib.SequenceMatcher(None, a, b).ratio()
    best = 0.0
    step = max(1, len(a) // 8)
    for k in range(0, len(b) - len(a) + 1, step):
        best = max(best, difflib.SequenceMatcher(None, a, b[k:k + len(a)]).ratio())
    return best


def audit_jobs(plan: dict, clips_dir: str, requested: str = "") -> list[dict]:
    """Explicit repair IDs include landed takes; omission retains missing-only intake."""
    jobs = plan.get("jobs", [])
    if requested:
        ids = {s.strip() for s in requested.split(",") if s.strip()}
        unknown = ids - {j["id"] for j in jobs}
        if not ids or unknown:
            raise ValueError("unknown or empty plan window selection: " + ", ".join(sorted(unknown)))
        return [j for j in jobs if j["id"] in ids]
    return [j for j in jobs if not os.path.isfile(os.path.join(clips_dir, f"{j['id']}.mp4"))]


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--project", required=True)
    ap.add_argument("--index", required=True)
    ap.add_argument("--transcripts", required=True)
    ap.add_argument("--out", default="")
    ap.add_argument("--json", default="")
    ap.add_argument("--windows", default="", help="comma-separated PLAN window IDs to review, including existing takes")
    ap.add_argument("--worlds-from", action="append", default=[],
                    help="another rap-mv project dir whose clips are in the pool: its plan names their worlds")
    a = ap.parse_args()
    P = os.path.abspath(a.project)
    build = os.path.join(P, "build")
    out = a.out or os.path.join(build, "reuse-review.html")
    plan = json.load(open(os.path.join(build, "plan.json"), encoding="utf-8"))
    brief = json.load(open(os.path.join(build, "vocal-brief.json"), encoding="utf-8"))
    bw = {w["id"]: w for w in brief.get("windows", [])}
    idx = json.load(open(a.index, encoding="utf-8"))
    rows = idx.get("clips") if isinstance(idx, dict) else idx
    tx = json.load(open(a.transcripts, encoding="utf-8"))
    tclips = tx.get("clips", {})
    song_words = tx.get("song", {}).get("words", [])
    clips_dir = os.path.join(plan["queueDir"], "clips") if plan.get("queueDir") else os.path.join(build, "queue", "explore", "clips")
    shots_dir = os.path.join(P, "references", "shots")

    # candidates: index rows joined with their transcripts. The indexer resolves symlinks,
    # so a pool entry (p1-w07.mp4 -> part 1's w07.mp4) is keyed back through its real path.
    real_to_cid = {}
    for cid, t_ in tclips.items():
        try:
            real_to_cid[os.path.realpath(t_["path"])] = cid
        except (KeyError, OSError):
            continue
    # the WORLD of a clip from another rap-mv project: its window's style in that project's
    # plan (a parked draw was for the same window, so the same world)
    world_of: dict[str, str] = {}
    for src in a.worlds_from:
        try:
            pl = json.load(open(os.path.join(src, "build", "plan.json"), encoding="utf-8"))
            tag = os.path.basename(os.path.normpath(src))
            for jj in pl.get("jobs", []):
                world_of[(tag, jj["id"])] = jj.get("styleId", "")
        except (OSError, ValueError):
            continue
    # A brief describes requested words, not observed words. Never replace a take's
    # transcript (or missing evidence) with lyrics or zero-valued word timestamps.
    cands = []
    for r in rows:
        cid = real_to_cid.get(os.path.realpath(r["clip"])) or os.path.splitext(os.path.basename(r["clip"]))[0]
        t = dict(tclips.get(cid) or {})
        if not r.get("styleId"):
            m_ = re.search(r"(w\d\d)", cid)
            src_tag = next((os.path.basename(os.path.normpath(s)) for s in a.worlds_from
                            if os.path.realpath(r["clip"]).startswith(os.path.realpath(s) + os.sep)), None)
            if m_ and src_tag:
                r["styleId"] = world_of.get((src_tag, m_.group(1)), "")
        cw = (words_of(" ".join(w.get("w", w.get("word", "")) for w in t["words"])) if "words" in t
              else words_of(t.get("text", "")))
        cands.append({**r, "cid": cid, "cwords": cw, "text": t.get("text", ""),
                      "hasTranscript": isinstance(t.get("words"), list) or bool(t.get("text")), "twords": t.get("words", []), "fitBy": {f["window"]: f for f in r.get("fits", [])}})

    try:
        pending = audit_jobs(plan, clips_dir, a.windows)
    except ValueError as exc:
        ap.error(str(exc))

    results, summary = [], {"pending": len(pending), "place": 0, "slide": 0, "recut": 0, "partial": 0, "eye": 0, "none": 0}
    for j in pending:
        jid = j["id"]
        w = bw.get(jid) or {}
        spans = [sp for sp in w.get("spans", []) if not sp.get("partialHead") and not sp.get("partialTail")]
        lines = [sp.get("text", "") for sp in spans]
        lw = words_of(" ".join(lines))
        sung = bool(j.get("voiceRef"))
        need = float(j["seconds"])
        world = j.get("styleId")
        verdicts = []
        for c in cands:
            secs = float(c.get("seconds") or 0)
            fit = c["fitBy"].get(jid) or {}
            corr, off = float(fit.get("corr") or 0), int(fit.get("offsetMs") or 0)
            cworld = c.get("styleId")
            heard = len(c["cwords"])
            v: dict = {"cid": c["cid"], "clip": c["clip"], "origin": c.get("origin"), "world": cworld,
                       "seconds": secs, "corr": corr, "offsetMs": off, "text": c["text"], "heard": heard}
            if not c["hasTranscript"]:
                v["verdict"], v["why"] = "eye", "no take transcript — inspect the existing footage; silence and lyrics are unknown"
            elif sung:
                frac = match_fraction(lw, c["cwords"]) if lw else 0.0
                # which of the window's lines does the clip carry, in order?
                carried = [ln for ln in lines if match_fraction(words_of(ln), c["cwords"]) >= WORD_MATCH_MIN]
                v.update({"wordMatch": round(frac, 2), "carried": carried})
                if frac >= WORD_MATCH_MIN and len(carried) == len(lines):
                    if corr >= BAR_CORR and abs(off) <= BAR_OFF_MS and secs + 0.05 >= need:
                        v["verdict"], v["why"] = "place", f"words {frac:.0%}, on the bar (corr {corr:.2f} @ {off:+d} ms), {secs:.1f}s ≥ {need:g}s"
                    elif corr >= 0.5 and abs(off) <= SLIDE_MS and secs + 0.05 >= need:
                        v["verdict"], v["why"] = "slide", f"words {frac:.0%}, corr {corr:.2f} @ {off:+d} ms — the cut stage can slide it"
                    else:
                        # a re-cut around the clip: where does the record sing this line, where does the clip?
                        first = lw[0]
                        rec_t = next((float(sw.get("t", sw.get("start", 0))) for sw in song_words
                                      if norm_latin(str(sw.get("w", sw.get("word", "")))).strip() == first
                                      and float(j["startSec"]) - 1.5 <= float(sw.get("t", sw.get("start", 0))) <= float(j["startSec"]) + need), None)
                        clip_t = next((float(cw_.get("t", cw_.get("start", 0))) for cw_ in c["twords"]
                                       if norm_latin(str(cw_.get("w", cw_.get("word", "")))).strip() == first), None)
                        if rec_t is not None and clip_t is not None:
                            new_start = round(rec_t - clip_t, 2)
                            v["recut"] = {"start": new_start, "seconds": secs, "recordFirstWordAt": rec_t, "clipFirstWordAt": clip_t}
                            v["verdict"], v["why"] = "recut", (f"words {frac:.0%} but off the bar (corr {corr:.2f} @ {off:+d} ms) — "
                                                              f"re-cut the window around the clip: start {new_start:.2f}s for {secs:.1f}s "
                                                              f"(record sings the first word at {rec_t:.2f}s, the clip at {clip_t:.2f}s)")
                        else:
                            v["verdict"], v["why"] = "recut", f"words {frac:.0%} but off the bar (corr {corr:.2f} @ {off:+d} ms); first-word times not found — align by eye"
                elif carried:
                    v["verdict"], v["why"] = "partial", f"carries {len(carried)}/{len(lines)} line(s): {' / '.join(carried)[:70]} — retain these lines; review an existing take or suitable B-roll over the remainder"
                else:
                    v["verdict"], v["why"] = "no", f"other words ({frac:.0%} match)" if heard else "no words heard (a dance take)"
            else:
                if heard > MAX_SILENT_WORDS:
                    v["verdict"], v["why"] = "no", f"sings {heard} words — a mouth on other words under the record"
                elif secs + 0.05 < need:
                    v["verdict"], v["why"] = "no", f"too short: {secs:.1f}s for a {need:g}s window"
                elif cworld and cworld != world:
                    v["verdict"], v["why"] = "no", f"other world ({cworld})"
                elif not cworld:
                    v["verdict"], v["why"] = "eye", f"{secs:.1f}s, silent; world unknown (not this lane's take) — judge the look by eye"
                else:
                    v["verdict"], v["why"] = "eye", f"same world, few or no words heard, {secs:.1f}s ≥ {need:g}s — check visible mouth movement before using as B-roll"
            if v["verdict"] != "no":
                v["reviewRequired"] = True
                v["cmd"] = ("Review source in/out, words, face and moving-picture coverage; retain the current edit. "
                            "For partial matches, audition existing B-roll over the uncovered phrase. "
                            "Apply only the reviewed edit with its source and timing recorded.")
            verdicts.append(v)
        order = {"place": 0, "slide": 1, "recut": 2, "partial": 3, "eye": 4, "no": 5}
        verdicts.sort(key=lambda v: (order[v["verdict"]], -v["corr"]))
        best = verdicts[0]["verdict"] if verdicts else "no"
        summary[{"place": "place", "slide": "slide", "recut": "recut", "partial": "partial", "eye": "eye", "no": "none"}[best]] += 1
        results.append({"window": jid, "sung": sung, "world": world, "startSec": j["startSec"], "seconds": need,
                        "onDisk": os.path.isfile(os.path.join(clips_dir, f"{jid}.mp4")),
                        "lines": lines, "best": best, "candidates": verdicts})

    # ---- page
    def strip_src(c: dict) -> str:
        s = c.get("strip") or ""
        return os.path.relpath(s, os.path.dirname(os.path.abspath(out))) if s and os.path.isfile(s) else ""

    strips = {c["cid"]: strip_src(c) for c in cands}
    parts = [
        '<div class="sum">'
        + "".join(f'<div class="box"><div class="n">{summary[k]}</div><div class="l">{lbl}</div></div>' for k, lbl in (
            ("pending", "windows reviewed"), ("place", "whole-take candidates"), ("slide", "timing-repair candidates"),
            ("recut", "with a re-cut (your eye)"), ("partial", "partial only"), ("eye", "needs inspection"), ("none", "nothing fits")))
        + "</div>"
        + '<p class="small">Sung windows need the WORDS and the timing; dance windows need the same world, silence and the length. '
        'The record plays over every window (AUDIO_MODE=record), so a reused take only has to LOOK right and, when it sings, mouth on the bar.</p>'
    ]
    for r in results:
        kf = os.path.join(shots_dir, f"{r['window']}-keyframe.png")
        kf_html = f'<img class="key" src="{html.escape(os.path.relpath(kf, os.path.dirname(os.path.abspath(out))))}" alt="keyframe">' if os.path.isfile(kf) else ""
        parts.append(f'<div class="win">{kf_html}<h2>{r["window"]} <span class="v v-{r["best"] if r["best"] != "no" else "no"}">{r["best"].upper() if r["best"] != "no" else "NOTHING FITS"}</span> '
                     f'<span class="small">{"sung" if r["sung"] else "dance"} · {html.escape(r["world"] or "")} · {r["startSec"]:.1f}s + {r["seconds"]:g}s</span></h2>'
                     f'<div class="lines">{html.escape(" / ".join(r["lines"])) or "<i>instrumental</i>"}</div><div class="cands">')
        shown = [v for v in r["candidates"] if v["verdict"] != "no"][:8] or r["candidates"][:3]
        for v in shown:
            img = strips.get(v["cid"], "")
            parts.append(
                f'<div class="cand">' + (f'<img src="{html.escape(img)}" alt="{v["cid"]}">' if img else "")
                + f'<div class="m"><b>{html.escape(v["cid"])}</b> <span class="v v-{v["verdict"]}">{v["verdict"].upper()}</span>'
                + f'<br><span class="small">{html.escape(v.get("origin") or "")} · {html.escape(v.get("world") or "world ?")} · {v["seconds"]:.1f}s · corr {v["corr"]:.2f} @ {v["offsetMs"]:+d} ms</span>'
                + f'<br><span class="tx">“{html.escape(v["text"][:90])}”</span>'
                + f'<br>{html.escape(v["why"])}'
                + (f'<div class="cmd">{html.escape(v["cmd"])}</div>' if v.get("cmd") else "")
                + "</div></div>")
        parts.append("</div></div>")
    doc = page("Reuse review", "Existing footage to review before a replacement render",
               "".join(parts), slug=os.path.basename(P), extra_css=CSS)
    open(out, "w", encoding="utf-8").write(doc)
    if a.json:
        json.dump({"summary": summary, "reviewOnly": True, "windows": results}, open(a.json, "w", encoding="utf-8"), indent=1, ensure_ascii=False)
    print(f"reuse_audit: {out} — " + ", ".join(f"{k} {v}" for k, v in summary.items()))
    for r in results:
        b = next((v for v in r["candidates"] if v["verdict"] == r["best"]), None)
        print(f"  {r['window']} {'sung ' if r['sung'] else 'dance'} {r['seconds']:g}s {r['best']:8} " + (f"{b['cid']} — {b['why'][:100]}" if b else ""))


if __name__ == "__main__":
    main()
