# SlasherPlay Assessment Makefile
# Workflow: .typ → .pdf → -compressed.pdf

SHELL := /bin/bash
.PHONY: all pdf compress clean help
.DEFAULT_GOAL := help

# Paths
REPO_ROOT   := ../..
FONT_PATH   := $(REPO_ROOT)/src/assets/lib/fonts
SRC         := slasherplay-assessment.typ
PDF         := slasherplay-assessment.pdf
COMPRESSED  := slasherplay-assessment-compressed.pdf

# Colors (printf-safe)
GREEN  := \\033[0;32m
YELLOW := \\033[0;33m
NC     := \\033[0m

# Tool checks
TYPST := $(shell command -v typst 2>/dev/null)

##@ Targets

all: compress ## Build PDF and compressed variant

pdf: $(PDF) ## Compile Typst source to PDF

compress: $(COMPRESSED) ## Compile then compress PDF with ghostscript

##@ Recipes

$(PDF): $(SRC) $(wildcard screenshots/*.png)
	@echo "→ Compiling $(PDF) (Typst)..."
	@test -n "$(TYPST)" || (echo "✗ typst not found in PATH" && exit 1)
	@typst compile $(SRC) $(PDF) --font-path $(FONT_PATH) --pdf-standard 1.7
	@test -f $(PDF) || (echo "✗ PDF generation failed" && exit 1)
	@printf "$(GREEN)✓$(NC) PDF built: $(PDF)\n"

$(COMPRESSED): $(PDF)
	@echo "→ Compressing $(PDF)..."
	@GS=""; \
	if command -v gs >/dev/null 2>&1; then GS="gs"; \
	elif command -v nix >/dev/null 2>&1; then GS="nix shell nixpkgs#ghostscript --command gs"; \
	fi; \
	if [ -n "$$GS" ]; then \
		$$GS -sDEVICE=pdfwrite -dCompatibilityLevel=1.7 -dPDFSETTINGS=/printer \
			-dNOPAUSE -dQUIET -dBATCH -sOutputFile=$(COMPRESSED) $(PDF); \
		printf "$(GREEN)✓$(NC) Compressed: $(COMPRESSED) ($$(du -sh $(COMPRESSED) | cut -f1))\n"; \
	else \
		printf "$(YELLOW)!$(NC) ghostscript not found; install with: nix shell nixpkgs#ghostscript\n"; \
		cp $(PDF) $(COMPRESSED); \
	fi

clean: ## Remove generated PDFs
	@rm -f $(PDF) $(COMPRESSED)
	@printf "$(GREEN)✓$(NC) Cleaned\n"

help: ## Show this help
	@echo ""
	@echo "  SlasherPlay Assessment"
	@echo ""
	@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*##/ { printf "  $(GREEN)%-12s$(NC) %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
	@echo ""
	@echo "  Note: PDF compression requires ghostscript (nix shell nixpkgs#ghostscript)"
	@echo ""
