#!/usr/bin/env python3
"""ingest_source.py — one canonical home for a film's SOURCE recording, from a URL or a file.

WHY. A film that copies a real music video ("follow the video") needs the original three
ways: its audio as the song the lane is cut to, its picture for the shot references and the
dance breakdown, and a SILENT copy for review players (a browser leaks a video element's
own audio on seek and fullscreen; the review pages route sound through the stems). Until
now the operator placed these by hand — the mp3 under artifacts/audio so the `song` stage
would skip the Suno buy, the mp4 wherever the pack's SHOT_REFS_VIDEO pointed — and every
film did it a little differently. This writes ONE layout and records what it did.

  references/source.mp4          as downloaded (bestvideo mp4 + bestaudio m4a, merged) — or the file given
  references/source.wav          pcm_s16le 44.1 kHz STEREO (vocal_map wants stereo; hook.wav stays mono)
  references/source-silent.mp4   720p H.264, -an — ZERO audio streams, asserted with ffprobe
  references/source.json         {url, extractor, id, title, durationSec, sha256:{mp4,wav}, downloadedAt,
                                  ytDlpVersion, songFrom}
  artifacts/audio/soundtrack-suno.mp3   320 kbps from source.wav, unless --no-song — the runner's $SONG,
                                  so the song stage finds it and buys nothing (as baby-nimbooda did by hand)

Idempotent by content: a second run with the same URL (source.json.id) or the same file
(sha) skips the download and the encodes. `--dry-run` prints the yt-dlp argv and writes
nothing. yt-dlp is shelled from PATH (skills/youtube-audio has its own general downloader
with no project convention; this one exists for the convention). `--cookies-from-browser`
is opt-in: on macOS it prompts for the Keychain.

Usage:
  ingest_source.py --project <dir> (--url <u> | --file <local.mp4|.mp3|.wav>)
                   [--cookies-from-browser chrome] [--no-song] [--dry-run]
Exit 0 · 2 bad inputs · 4 yt-dlp / ffmpeg failed · 5 the silent copy still carries audio.
"""
from __future__ import annotations

import argparse
import datetime as dt
import hashlib
import json
import os
import shutil
import subprocess
import sys

FFMPEG = os.environ.get("FFMPEG_FULL") or os.environ.get("FFMPEG_BIN") or "ffmpeg"
FFPROBE = os.environ.get("FFPROBE_BIN") or "ffprobe"
YTDLP = os.environ.get("YTDLP_BIN") or "yt-dlp"
SONG_REL = os.path.join("artifacts", "audio", "soundtrack-suno.mp3")   # the runner's $SONG


def say(msg: str) -> None:
    print(f"ingest_source: {msg}", file=sys.stderr)


def sha256_file(path: str) -> str:
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


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 round(float(r.stdout.strip()), 3)
    except ValueError:
        return 0.0


def audio_streams(path: str) -> int:
    r = subprocess.run([FFPROBE, "-v", "error", "-select_streams", "a", "-show_entries", "stream=index",
                        "-of", "csv=p=0", path], capture_output=True, text=True)
    return len([l for l in r.stdout.splitlines() if l.strip()])


def has_video(path: str) -> bool:
    r = subprocess.run([FFPROBE, "-v", "error", "-select_streams", "v", "-show_entries", "stream=codec_type",
                        "-of", "csv=p=0", path], capture_output=True, text=True)
    return "video" in r.stdout


def channels(path: str) -> int:
    r = subprocess.run([FFPROBE, "-v", "error", "-select_streams", "a:0", "-show_entries", "stream=channels",
                        "-of", "csv=p=0", path], capture_output=True, text=True)
    try:
        return int(r.stdout.strip())
    except ValueError:
        return 0


def ytdlp_argv(url: str, out: str, cookies: str | None) -> list[str]:
    return [YTDLP, "-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
            "--merge-output-format", "mp4", "--no-playlist", "-o", out] \
        + (["--cookies-from-browser", cookies] if cookies else []) + [url]


def ytdlp_info(url: str, cookies: str | None) -> dict:
    r = subprocess.run([YTDLP, "--dump-single-json", "--no-playlist"]
                       + (["--cookies-from-browser", cookies] if cookies else []) + [url],
                       capture_output=True, text=True)
    if r.returncode != 0:
        raise RuntimeError(r.stderr.strip()[-400:])
    d = json.loads(r.stdout)
    return {"id": d.get("id"), "extractor": d.get("extractor_key") or d.get("extractor"),
            "title": d.get("title"), "durationSec": d.get("duration"), "uploader": d.get("uploader")}


def run(cmd: list[str], what: str) -> None:
    r = subprocess.run(cmd, capture_output=True, text=True)
    if r.returncode != 0:
        say(f"{what} failed: {r.stderr.strip()[-600:]}")
        raise SystemExit(4)


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--project", required=True)
    src = ap.add_mutually_exclusive_group(required=True)
    src.add_argument("--url")
    src.add_argument("--file")
    ap.add_argument("--cookies-from-browser", default=os.environ.get("SOURCE_COOKIES") or None)
    ap.add_argument("--no-song", action="store_true", help="do not write artifacts/audio/soundtrack-suno.mp3")
    ap.add_argument("--dry-run", action="store_true")
    a = ap.parse_args()

    project = os.path.abspath(a.project)
    refs = os.path.join(project, "references")
    mp4 = os.path.join(refs, "source.mp4")
    wav = os.path.join(refs, "source.wav")
    silent = os.path.join(refs, "source-silent.mp4")
    meta_path = os.path.join(refs, "source.json")
    song = os.path.join(project, SONG_REL)
    if a.file and not os.path.isfile(a.file):
        say(f"--file not on disk: {a.file}")
        return 2
    if not os.path.isdir(project):
        say(f"--project is not a directory: {project}")
        return 2

    prior = None
    if os.path.isfile(meta_path):
        try:
            prior = json.load(open(meta_path, encoding="utf-8"))
        except ValueError:
            prior = None

    # --- 1. the source itself ------------------------------------------------------------
    info: dict = {}
    if a.url:
        if a.dry_run:
            say("DRY RUN — would run: " + " ".join(ytdlp_argv(a.url, mp4, a.cookies_from_browser)))
            say(f"then write {wav}, {silent}, {meta_path}" + ("" if a.no_song else f", {song}") + "; nothing written")
            return 0
        if shutil.which(YTDLP) is None:
            say(f"{YTDLP} is not on PATH (pipx install yt-dlp)")
            return 2
        try:
            info = ytdlp_info(a.url, a.cookies_from_browser)
        except RuntimeError as exc:
            say(f"yt-dlp could not read the URL: {exc}")
            return 4
        if prior and prior.get("id") == info.get("id") and os.path.isfile(mp4) and prior.get("sha256", {}).get("mp4") == sha256_file(mp4):
            say(f"already ingested {info.get('id')} ({prior.get('title')}) — the download is skipped")
        else:
            os.makedirs(refs, exist_ok=True)
            run(ytdlp_argv(a.url, mp4, a.cookies_from_browser), "yt-dlp download")
        info["url"] = a.url
    else:
        if a.dry_run:
            say(f"DRY RUN — would copy {a.file} → {mp4} and write {wav}, {silent}, {meta_path}"
                + ("" if a.no_song else f", {song}") + "; nothing written")
            return 0
        os.makedirs(refs, exist_ok=True)
        src_sha = sha256_file(a.file)
        if not (os.path.isfile(mp4) and sha256_file(mp4) == src_sha):
            shutil.copyfile(a.file, mp4)
        info = {"url": None, "extractor": "local-file", "id": src_sha[:12], "title": os.path.basename(a.file),
                "sourceFile": os.path.abspath(a.file)}
    if not os.path.isfile(mp4):
        say(f"no source at {mp4} after the download")
        return 4
    info["durationSec"] = probe_seconds(mp4)
    if not has_video(mp4):
        say("the source has no video stream — an audio-only source has no picture for shot references; "
            "source.wav and the song are still written")

    # --- 2. stereo wav for the lane's stereo consumers (vocal_map) -------------------------
    if not (os.path.isfile(wav) and prior and prior.get("sha256", {}).get("wav") == sha256_file(wav) and prior.get("sha256", {}).get("mp4") == sha256_file(mp4)):
        run([FFMPEG, "-v", "error", "-y", "-i", mp4, "-vn", "-ac", "2", "-ar", "44100", "-c:a", "pcm_s16le", wav],
            "source.wav encode")
    if channels(wav) != 2:
        say(f"source.wav is not stereo ({channels(wav)} ch)")
        return 4

    # --- 3. the silent 720p copy for review players --------------------------------------
    if has_video(mp4):
        if not (os.path.isfile(silent) and audio_streams(silent) == 0 and prior and prior.get("sha256", {}).get("mp4") == sha256_file(mp4)):
            run([FFMPEG, "-v", "error", "-y", "-i", mp4, "-vf", "scale=-2:720", "-c:v", "libx264", "-preset", "fast",
                 "-crf", "22", "-pix_fmt", "yuv420p", "-an", "-movflags", "+faststart", silent], "source-silent.mp4 encode")
        if audio_streams(silent) != 0:
            say(f"source-silent.mp4 still carries {audio_streams(silent)} audio stream(s) — a review player would leak it")
            return 5

    # --- 4. the song the lane is cut to ----------------------------------------------------
    song_from = None
    if not a.no_song:
        if os.path.isfile(song) and not (prior and prior.get("songFrom") == "references/source.wav"):
            say(f"{SONG_REL} already exists and was not written by this ingest — left as it is (use --no-song to silence this)")
        else:
            os.makedirs(os.path.dirname(song), exist_ok=True)
            run([FFMPEG, "-v", "error", "-y", "-i", wav, "-c:a", "libmp3lame", "-b:a", "320k", song], "song mp3 encode")
            song_from = "references/source.wav"

    meta = {"schemaVersion": 1, **info,
            "sha256": {"mp4": sha256_file(mp4), "wav": sha256_file(wav)},
            "audioStreamsInSilentCopy": audio_streams(silent) if os.path.isfile(silent) else None,
            "downloadedAt": dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"),
            "ytDlpVersion": (subprocess.run([YTDLP, "--version"], capture_output=True, text=True).stdout.strip()
                             if a.url and shutil.which(YTDLP) else None),
            "songFrom": song_from or (prior or {}).get("songFrom")}
    tmp = meta_path + ".tmp"
    with open(tmp, "w", encoding="utf-8") as fh:
        json.dump(meta, fh, indent=1)
    os.replace(tmp, meta_path)
    say(f"references/source.mp4 ({meta['durationSec']:.1f} s) · source.wav (stereo) · "
        + ("source-silent.mp4 (0 audio streams) · " if os.path.isfile(silent) else "")
        + (f"{SONG_REL} written" if song_from else "song left as it was") + f" → {meta_path}")
    return 0


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