.PHONY: help serve size validate tidy video video-list video-frames video-bundle clean

# ASCII render resolution (override on the command line):
#   make video NAME=nature1 SIZE=160x48 SCALE=640
SIZE  ?= 80x24
SCALE ?= 320
FPS   ?= 12

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

serve: ## Start a local static server on :8000
	@command -v python3 >/dev/null 2>&1 && \
	  python3 -m http.server 8000 || \
	  (command -v npx >/dev/null 2>&1 && npx --yes http-server -p 8000) || \
	  echo "Install python3 or node to serve locally."

size: ## Report file sizes
	@echo "HTML:"
	@find . -maxdepth 2 -name "*.html" -printf "  %-30p %s bytes\n"
	@echo
	@echo "CSS:"
	@find static/css -name "*.css" -printf "  %-30p %s bytes\n"
	@echo
	@echo "Images (svg):"
	@find static/img -name "*.svg" -printf "  %-30p %s bytes\n"

validate: ## Quick HTML sanity check (counts opening vs closing tags)
	@for f in *.html; do \
	  open=$(grep -oE '<[a-zA-Z][a-zA-Z0-9-]*' "$$f" | grep -vE '^<(br|hr|img|input|meta|link|source|area|base|col|embed|param|track|wbr|!--)' | sed 's/^<//' | sort -u); \
	  close=$$(grep -oE '</[a-zA-Z][a-zA-Z0-9-]*' "$$f" | sed 's/^<\///' | sort -u); \
	  echo "$$f"; \
	  diff <(echo "$$open") <(echo "$$close") || true; \
	done

tidy: ## Run html-tidy on every .html
	@for f in *.html; do echo "--- $$f ---"; tidy -e -q $$f || true; done

video: ## Convert a video from video/<name>.mp4 into ASCII bundle. Usage: make video NAME=nature1
	@test -n "$(NAME)" || { echo "Usage: make video NAME=<name>   (without .mp4)"; exit 1; }
	@test -f "video/$(NAME).mp4" || { echo "video/$(NAME).mp4 not found. Available:"; $(MAKE) video-list; exit 1; }
	@echo "==> Processing video/$(NAME).mp4"
	@$(MAKE) video-frames NAME=$(NAME) --no-print-directory
	@$(MAKE) video-bundle NAME=$(NAME) --no-print-directory
	@echo "==> Done. static/js/mountain.js is now the ASCII version of $(NAME)."

video-list: ## List available videos in video/
	@ls -1 video/*.mp4 2>/dev/null | sed 's|video/||; s|\.mp4$$||' || echo "  (no videos found)"

video-frames: ## Extract PNG frames from video/<NAME>.mp4 and chafa each to ASCII
	@test -n "$(NAME)" || { echo "NAME is required"; exit 1; }
	@mkdir -p build/$(NAME)
	@command -v ffmpeg >/dev/null 2>&1 || \
	  { echo "ffmpeg not found. Run via: nix develop -c make video-frames NAME=$(NAME)"; exit 1; }
	@command -v chafa >/dev/null 2>&1 || \
	  { echo "chafa not found. Run via: nix develop -c make video-frames NAME=$(NAME)"; exit 1; }
	@echo "  extracting frames at $(FPS) fps from video/$(NAME).mp4"
	@ffmpeg -y -loglevel error -i video/$(NAME).mp4 -vf "fps=$(FPS),scale=$(SCALE):-1" build/$(NAME)/frame-%03d.png
	@echo "  chafa: $(SIZE) ascii per frame"
	@for f in build/$(NAME)/frame-*.png; do \
	  chafa -f symbols -c none -s $(SIZE) --symbols ascii --animate off "$$f" > "$${f%.png}.txt"; \
	done
	@echo "  rendered $$(($$(ls build/$(NAME)/frame-*.png | wc -l))) PNG + ASCII frames in build/$(NAME)/"

video-bundle: ## Bundle ASCII frames from build/<NAME>/ into static/js/mountain.js
	@test -n "$(NAME)" || { echo "NAME is required"; exit 1; }
	@python3 tools/build_mountain_js.py build/$(NAME) static/js/mountain.js
	@echo "  bundled static/js/mountain.js (regenerated from build/$(NAME)/)"

clean: ## Remove build artefacts
	@rm -rf bin/ node_modules/ dist/
