# reelkit: one command per stage. `make help` lists them. REEL defaults to the sample in example/.
#   make all                        voice -> gate -> sound -> render -> finish -> qa for the sample
#   make new SLUG=my-topic          start a new reel in reels/my-topic/
#   make all REEL=reels/my-topic    build it
# No make (Windows)? Each target is one command; run the line under it by hand (see README).

REEL ?= example
PORT ?= 8860
PY   ?= .venv/bin/python
T    ?= 0,1.5,3,5,7,9,11,13
AT   ?= 0

.PHONY: help setup doctor new voice gate sound stills preview render finish qa cover all clean

help:
	@echo "make setup                 install Python + Node dependencies (once)"
	@echo "make doctor                check ffmpeg, node, python env, browser"
	@echo "make new SLUG=my-topic     new reel in reels/my-topic (script.json + its own scene/)"
	@echo "make voice   REEL=...      TTS takes for every line"
	@echo "make gate    REEL=...      Whisper exact-match gate -> voiceover.wav + timeline.json"
	@echo "make stills  REEL=... T=0,2,4   frames + contact sheet to look at while building the scene"
	@echo "make preview REEL=...      open the scene in your browser with the voice"
	@echo "make sound   REEL=...      original music bed + SFX"
	@echo "make render  REEL=...      frame-by-frame render -> picture.mp4"
	@echo "make finish  REEL=...      mix to -14 LUFS + encode -> final.mp4"
	@echo "make qa      REEL=...      automated QA -> qa.json + contact-sheet.jpg"
	@echo "make cover   REEL=... AT=1.2   cover.jpg from the final video"
	@echo "make all     REEL=...      voice gate sound render finish qa"

setup:
	test -d .venv || uv venv --python 3.12 .venv
	VIRTUAL_ENV="$(CURDIR)/.venv" uv pip install -r requirements.txt
	npm install --no-audit --no-fund
	npx playwright install chromium

doctor:
	@command -v ffmpeg >/dev/null && ffmpeg -version | head -1 || echo "MISSING ffmpeg (brew install ffmpeg)"
	@command -v ffprobe >/dev/null || echo "MISSING ffprobe (comes with ffmpeg)"
	@command -v node >/dev/null && node -e 'const v=+process.versions.node.split(".")[0]; console.log("node " + process.version + (v < 18 ? "  TOO OLD: need 18+" : ""))' || echo "MISSING node 18+ (brew install node)"
	@test -x $(PY) && $(PY) -c "import kokoro, faster_whisper, soundfile; print('python env ok:', __import__('sys').version.split()[0])" || echo "MISSING python env: run make setup"
	@test -d node_modules/three && test -d node_modules/playwright && echo "node modules ok" || echo "MISSING node modules: run make setup"
	@node -e "import('playwright').then(p => console.log('browser:', require('fs').existsSync(p.chromium.executablePath()) ? 'ok' : 'MISSING: npx playwright install chromium'))" 2>/dev/null || true

new:
	@test -n "$(SLUG)" || { echo "usage: make new SLUG=my-topic"; exit 2; }
	@test ! -e "reels/$(SLUG)" || { echo "reels/$(SLUG) already exists"; exit 2; }
	mkdir -p "reels/$(SLUG)/assets"
	cp example/script.json "reels/$(SLUG)/script.json"
	cp -R engine/scene "reels/$(SLUG)/scene"
	@echo "Created reels/$(SLUG). Edit script.json (and scene/scene.js for the world), then: make all REEL=reels/$(SLUG)"

voice:
	$(PY) engine/voice.py $(REEL)

gate:
	$(PY) engine/asr_gate.py $(REEL)

sound:
	$(PY) engine/sound.py $(REEL)

stills:
	node engine/render.mjs $(REEL) --stills $(T) --port $(PORT)

preview:
	node engine/render.mjs $(REEL) --serve --port $(PORT)

render:
	node engine/render.mjs $(REEL) --port $(PORT)

finish:
	sh engine/finish.sh $(REEL)

qa:
	$(PY) engine/qa.py $(REEL)

cover:
	ffmpeg -v error -y -ss $(AT) -i $(REEL)/final.mp4 -frames:v 1 -q:v 2 $(REEL)/cover.jpg && echo "wrote $(REEL)/cover.jpg"

all: voice gate sound render finish qa

clean:
	cd $(REEL) && rm -rf takes stills frames .work takes.json asr-cache.json gate.json sound.json voiceover.wav \
	  music.wav sfx.wav final-mix.wav picture.mp4 final.mp4 contact-sheet.jpg cover.jpg qa.json timeline.json
