# Story Loom Platform — Makefile
# ───────────────────────────────

# Auto-load .env if present
ifneq (,$(wildcard .env))
  include .env
  export
endif

PYTHON ?= python3
TEMPLATE ?= fengshui-monday-water

.PHONY: install install-render test lint demo demo-play demo-list clean publish help

help: ## Show available targets
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2}'

install: ## Install package in editable mode
	$(PYTHON) -m pip install -e .
	@command -v npm >/dev/null 2>&1 && npm install || true

install-render: ## Install with render extras (TTS, image-gen)
	$(PYTHON) -m pip install -e ".[render]"

test: ## Run full test suite
	$(PYTHON) -m pytest tests/

lint: ## Run ruff linter
	$(PYTHON) -m ruff check story_loom/ tests/

demo: ## Render a demo video (TEMPLATE=<id>)
	$(PYTHON) -m story_loom.runner demo --template $(TEMPLATE)

demo-play: ## Open the rendered demo video
	@MP4="output/demo/$(TEMPLATE)/demo.mp4"; \
	if [ -f "$$MP4" ]; then open "$$MP4"; \
	else echo "No video found at $$MP4 — run 'make demo' first."; exit 1; fi

demo-list: ## List available template IDs
	$(PYTHON) -m story_loom.runner demo --list

clean: ## Remove generated output
	rm -rf output/

publish: ## Build and publish package to PyPI
	rm -rf dist/
	$(PYTHON) -m pip install --quiet build twine
	$(PYTHON) -m build
	$(PYTHON) -m twine upload dist/*
