#!/usr/bin/env python3
"""Load ALL remaining Record Store Guy video shots into the flowrinse queue
(~/Library/Application Support/flowrinse/queue.jsonl) for the Flow Auto-Pilot
extension. Character cards can't be attached via the queue, so each character's
full visual description is embedded in the prompt text. Fast shots first,
Quality-tier last. Skips ids already present in queue or results."""
import json, sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from make_sheets import S, SETS, STYLE  # (sheet, id, set, chars, tier, text)

FLOWRINSE = Path.home() / "Library/Application Support/flowrinse"
DEST = str(Path.home() / "Documents/recordstoreguy/flow-vids-RSG")

CHAR = {
 "Dez": "Dez is a lanky 20-something record store clerk: tall messy black quiff, olive skin, thick eyebrows, open red flannel shirt over a plain faded black t-shirt, rolled dark jeans, scuffed white hi-top sneakers, oversized silver headphones around his neck, rubber-limbed expressive acting.",
 "The Dead Wax": "The Dead Wax is a towering mass of glossy black molten liquid vinyl dripping upward, with exactly four dripping tonearm limbs with chrome needle-tip headshells and one large slowly spinning vinyl-record face with a hypnotic red spiral label for an eye, no mouth.",
 "Mildred": "Mildred is a fat gray-striped tabby store cat with half-closed judgmental eyes and a white chest.",
 "The Boring Guy": "The Boring Guy is a beige man in his 50s: tucked-in taupe polo over a modest belly, pleated khakis, gray combover, lanyard with a blank badge, reusable coffee cup, entirely unremarkable face.",
 "The Name Dropper": "The Name Dropper is a smug man in his 30s: expensive vintage brown leather jacket over a plain black t-shirt, designer glasses, groomed stubble, smartphone in hand, permanent smirk.",
 "The Postman": "The Postman is a postal worker in an immaculate blue uniform: pressed shorts with a knife-edge crease, high white socks, polished black shoes, blue postal cap, mirrored aviator sunglasses, granite jaw, enormous brown leather parcel satchel.",
 "The Accountant": "The Accountant is a gaunt figure in a slightly-too-tight gray wool suit with thin dark tie and sleeve garters, green translucent banker's visor, round glasses catching the light so his eyes are never visible, red pen behind his ear, leather ledger under one arm.",
}

# Track-1 remainder + reshoots not present in make_sheets data:
T1_EXTRA = [
 ("t1s2_death","store",["Dez"],"fast","The giant rolling vinyl record catches Dez mid-stride and rolls straight over him with a comic squash; it rolls away revealing Dez flattened into the checkerboard floor like a pressed record, dazed, one hand rising weakly. The clip ends held on this frame. Static wide shot."),
 ("t1s3_action","store",["Dez"],"fast","Long whipping tentacles of brown cassette tape burst out of a bargain bin and lash at Dez's ankles; he leaps sideways over a sweeping tape tendril at the last moment. Tracking medium-wide shot, low angle."),
 ("t1s3_death","store",["Dez"],"fast","Cassette tape tentacles catch Dez and whirl around him in a blur, wrapping him head to toe into a mummy of shiny brown tape; only his eyes are visible, blinking twice. The clip ends held on this frame. Static medium shot."),
 ("t1s4_action","store",["Dez"],"fast","The ceiling fan rips loose from the ceiling and helicopters through the aisle at head height, shredding paper; Dez ducks flat to the checkerboard floor as it screams over him. Static wide shot."),
 ("t1s4_death_reshoot","store",["Dez"],"fast","A flying ceiling fan scoops Dez up and flings him across the store; he crash-lands headfirst into a large wooden bargain bin with his legs sticking straight up, kicking once, then still. The clip ends held on this frame. Static wide shot."),
 ("t2s3_reshoot","sea",["Dez"],"fast","Dez sprints and leaps across a chain of half-sunken wooden record crates while three sharks circle and lunge at his heels; each shark's dorsal fin is a black vinyl record with a bright red circular label at its center, clearly visible cutting through the water. Tracking wide shot moving with him."),
]

def build(sid, st, chars, tier, text):
    parts = [SETS[st]] + [CHAR[c] for c in chars] + [text, STYLE]
    return {"id": f"rsg_{sid}", "kind": "video",
            "model": "Veo 3.1 - Quality" if tier == "qual" else "Veo 3.1 - Fast",
            "prompt": " ".join(parts), "category": "rsg", "dest": DEST}

def main():
    done = set()
    for f in ("queue.jsonl", "results.jsonl"):
        p = FLOWRINSE / f
        if p.exists():
            for line in p.read_text().splitlines():
                try: done.add(json.loads(line).get("id"))
                except Exception: pass
    shots = [(sid, st, ch, tier, tx) for (_, sid, st, ch, tier, tx) in S] + T1_EXTRA
    fast = [s for s in shots if s[3] != "qual"]
    qual = [s for s in shots if s[3] == "qual"]
    n = 0
    with open(FLOWRINSE / "queue.jsonl", "a") as f:
        for sid, st, ch, tier, tx in fast + qual:
            t = build(sid, st, ch, tier, tx)
            if t["id"] in done: continue
            f.write(json.dumps(t) + "\n"); n += 1
    print(f"enqueued {n} video tasks ({len(fast)} fast-tier first, {len(qual)} quality-tier last, {len(done)} already known/skipped)")

if __name__ == "__main__":
    main()
