#!/usr/bin/env python3
"""fit_takes.py — fit every take to the record's tempo and line onset (AUDIO_MODE=vocal-swap).

A take sings its line its own way: 0-18 % slower than the record on the free lane, and
its first word sits 0.3-1.1 s into the clip (the head pad plus the model's own lead-in).
Under vocal-swap the film carries the take's voice, so the song stopped and started at
every cut — the voice ended late, then sat silent through the next take's lead-in
(operator, 2026-08-31 09:10: "it kind of stops and starts … so the original song flows").

The fit, per synced window, picture and voice TOGETHER (sync by construction survives):
    head = the take's first-word onset          -> trimmed off, so the first word lands
                                                   where the record's line starts
    rate = take's sung span / record's sung span -> setpts + atempo (pitch kept), clamped to
                                                   [1.0, MAX_RATE] and to what still covers
                                                   the window (the assembler refuses a clip
                                                   shorter than its cut)
The fitted copy keeps the clip's basename (its job id) in --out-dir; the selection is
rewritten onto it (fittedFrom / fitHead / fitRate / durationSec) and the take's words are
mapped for the consumers downstream (join_cuts, swap_words) into <out-dir>/words.json:
    t' = (t - head) / rate

Usage:
  fit_takes.py --selection build/selection-<sid>.json --plan build/plan.json \
               --store artifacts/clip-transcripts.json --out-dir build/fitted-<sid> \
               --out-selection build/selection-<sid>.json [--json build/fits-<sid>.json] [--fps 24] [--max-rate 1.25]
"""
from __future__ import annotations

import argparse
import json
import os
import subprocess
import sys

MAX_RATE = 1.25
LAST_WORD_S = 0.40
LEAD_S = 0.20        # air kept before the first word (whisper onsets sit a little late)
MIN_RATE_STEP = 1.01  # below this the retime is not worth a re-encode
COVER_MARGIN_S = 0.25 # the fitted copy must outlast its window by this much: a re-encode lands a
                      # frame or two short, and a clip with NO slack (the synthetic dry run's, or a
                      # provider that returns exactly N s) then failed the assembler by 0.02 s


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


def plan_fits(vocal_map: list[dict], clips: dict, clip_words: dict, jobs: dict, durations: dict,
              *, max_rate: float = MAX_RATE, last_word_s: float = LAST_WORD_S, lead_s: float = LEAD_S) -> list[dict]:
    """Pure. One row per synced window that has a take with words:
    {window, clip, head, rate, takeSung, recordSung, fittedLen, why}."""
    rows = []
    for w in vocal_map:
        # a COVER's exemption is about the sync measurement, not the fit: skipping the
        # window here left its path on the raw render, which the cover stage then
        # overwrote with a stale composite (review finding, 2026-09-02)
        if w.get("lipSyncExempt") and w.get("lipSyncExemptBy") != "cover":
            continue
        path = clips.get(w.get("performerClip") or "")
        if not path:
            continue
        job = jobs.get(os.path.splitext(os.path.basename(path))[0]) or {}
        words = clip_words.get(path) or []
        if not job.get("voiceRef") or len(words) < 2 or not job.get("sungSec"):
            continue
        # whisper pins a clip's first word to t=0; the second onset minus a syllable is the
        # honest first-word time, never earlier than the plan's head pad
        first = max(float(job.get("trimStartSec") or 0.0), float(words[1]["t"]) - 0.25) if float(words[1]["t"]) > 0.5 \
            else float(job.get("trimStartSec") or 0.0)
        head = round(max(0.0, first - lead_s), 3)
        last = float(words[-1]["t"]) + last_word_s
        take_sung = last - first
        record_sung = float(job["sungSec"])
        rate = take_sung / record_sung if record_sung > 0 else 1.0
        win = float(w["end"]) - float(w["start"])
        length = float(durations.get(path) or job.get("seconds") or 0.0)
        # the lead-in trim alone must leave the copy covering its window (+ margin): a
        # 0.89 s head off a 12.04 s clip for an 11.21 s window did not (dry run, 2026-08-31)
        if length > 0 and win > 0:
            head = round(max(0.0, min(head, length - win - COVER_MARGIN_S)), 3)
        cover_cap = (length - head) / (win + COVER_MARGIN_S) if win > 0 else max_rate   # fitted clip must still cover the window
        why = []
        if rate > max_rate:
            why.append(f"rate {rate:.2f} capped at {max_rate:.2f}")
        if rate > cover_cap:
            why.append(f"rate {min(rate, max_rate):.2f} capped at {cover_cap:.2f} so the clip still covers its {win:.2f}s window")
        rate = max(1.0, min(rate, max_rate, cover_cap))
        if rate < MIN_RATE_STEP:
            rate = 1.0
        rows.append({"window": w.get("performerClip"), "clip": path, "head": head, "rate": round(rate, 4),
                     "takeSung": round(take_sung, 3), "recordSung": round(record_sung, 3),
                     "fittedLen": round((length - head) / rate, 3) if length else None,
                     "why": "; ".join(why) or ("on speed, lead-in trimmed" if rate == 1.0 else "fitted to the record's tempo")})
    return rows


def fitted_words(words: list[dict], head: float, rate: float) -> list[dict]:
    out = []
    for i, w in enumerate(words):
        t = float(w["t"])
        if i == 0 and t == 0.0:
            t = head + LEAD_S            # whisper's pinned first word: it is the first word, at the lead-in
        if t < head:
            continue
        out.append({"t": round((t - head) / rate, 3), "w": w["w"]})
    return out


def fit_clip(src: str, dst: str, head: float, rate: float, fps: int) -> None:
    cmd = ["ffmpeg", "-v", "error", "-y", "-ss", f"{head:.3f}", "-i", src]
    vf = [f"setpts=PTS/{rate:.4f}"] if rate != 1.0 else []
    af = [f"atempo={rate:.4f}"] if rate != 1.0 else []
    if vf:
        cmd += ["-vf", ",".join(vf)]
    if af:
        cmd += ["-af", ",".join(af)]
    cmd += ["-r", str(fps), "-c:v", "libx264", "-preset", "veryfast", "-crf", "17", "-pix_fmt", "yuv420p",
            "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", dst]
    r = subprocess.run(cmd, capture_output=True, text=True)
    if r.returncode:
        raise SystemExit(f"fit_takes: ffmpeg failed on {src}: {r.stderr[-400:]}")


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--selection", required=True)
    ap.add_argument("--plan", required=True)
    ap.add_argument("--store", required=True)
    ap.add_argument("--out-dir", required=True)
    ap.add_argument("--out-selection", required=True)
    ap.add_argument("--json", default="")
    ap.add_argument("--fps", type=int, default=24)
    ap.add_argument("--max-rate", type=float, default=MAX_RATE)
    a = ap.parse_args()
    sel = json.load(open(a.selection, encoding="utf-8"))
    jobs = {j["id"]: j for j in json.load(open(a.plan, encoding="utf-8")).get("jobs", [])}
    store = json.load(open(a.store, encoding="utf-8")).get("clips") or {}
    words_by_path = {e["path"]: e.get("words") or [] for e in store.values() if isinstance(e, dict) and e.get("path")}
    # sources: a previous fit (fittedFrom) or join (joinedFrom) is undone — the fit is from the original
    by_id = {c["id"]: c for c in sel["clips"]}
    for c in sel["clips"]:
        src = c.get("fittedFrom") or c.get("joinedFrom") or c["path"]
        c["path"] = src
        for k in ("fittedFrom", "fitHead", "fitRate", "joinedFrom", "joinedShift"):
            c.pop(k, None)
    clips = {cid: c["path"] for cid, c in by_id.items()}
    durations = {p: probe_seconds(p) for p in set(clips.values())}
    rows = plan_fits(sel["vocalMap"], clips, words_by_path, jobs, durations, max_rate=a.max_rate)
    os.makedirs(a.out_dir, exist_ok=True)
    extra = {}
    for r in rows:
        c = by_id[r["window"]]
        if r["head"] <= 0.0 and r["rate"] == 1.0:
            continue
        dst = os.path.join(a.out_dir, os.path.basename(r["clip"]))
        fit_clip(r["clip"], dst, r["head"], r["rate"], a.fps)
        # THE COPY MUST COVER ITS WINDOW. Measure what ffmpeg actually wrote; if it is
        # short, ease the rate once (the encode's own rounding), and if still short give
        # up the retime (head trim only — the source covered the window at select).
        win = next((float(w["end"]) - float(w["start"]) for w in sel["vocalMap"] if w.get("performerClip") == r["window"]), 0.0)
        got = probe_seconds(dst)
        if got < win + 0.02 and r["rate"] > 1.0:
            eased = round(max(1.0, r["rate"] * got / (win + 0.12)), 4)
            print(f"  {r['window']}: fitted copy {got:.2f}s < window {win:.2f}s — easing rate x{r['rate']} -> x{eased}", file=sys.stderr)
            r["rate"] = eased; r["why"] += f"; eased to x{eased} so the copy covers its window"
            fit_clip(r["clip"], dst, r["head"], r["rate"], a.fps)
            got = probe_seconds(dst)
        if got < win + 0.02:
            print(f"  {r['window']}: still {got:.2f}s < window {win:.2f}s — keeping the source's speed, lead-in only", file=sys.stderr)
            r["rate"] = 1.0; r["why"] += "; retime dropped (copy could not cover the window)"
            fit_clip(r["clip"], dst, r["head"], 1.0, a.fps)
            got = probe_seconds(dst)
        c.update({"fittedFrom": r["clip"], "fitHead": r["head"], "fitRate": r["rate"], "path": dst,
                  "durationSec": round(got, 3)})
        extra[dst] = fitted_words(words_by_path.get(r["clip"]) or [], r["head"], r["rate"])
        # the fitted copy already starts at the first word: the assembler (which honours
        # headSec since 2026-08-31) must not skip a head that is no longer there
        for w in sel["vocalMap"]:
            if w.get("performerClip") == r["window"]:
                w["headSec"] = 0.0
    json.dump(sel, open(a.out_selection, "w", encoding="utf-8"), indent=1)
    json.dump(extra, open(os.path.join(a.out_dir, "words.json"), "w", encoding="utf-8"), indent=1)
    if a.json:
        json.dump({"fits": rows, "outDir": a.out_dir}, open(a.json, "w", encoding="utf-8"), indent=1)
    for r in rows:
        print(f"  {r['window']:<4} {os.path.basename(r['clip']):<9} head -{r['head']:.2f}s  rate x{r['rate']:.3f}  "
              f"take {r['takeSung']:.2f}s -> record {r['recordSung']:.2f}s  {r['why']}", file=sys.stderr)
    print(f"fit_takes: {len(extra)} take(s) fitted -> {a.out_dir} · words -> {os.path.join(a.out_dir, 'words.json')}", file=sys.stderr)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
