# comfyui-json-nodes — build pipeline
# Usage: make [target]
#   make ci       — build + typecheck + lint + test (CI-safe)
#   make release  — format + ci

SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c
.ONESHELL:
.PHONY: build typecheck lint format test integration ci release clean

BUN ?= bun

# ─── Build ──────────────────────────────────────────────
build:
	@echo "═══ Frontend build (bun) ═══"
	"$(BUN)" run bun.build.ts
	@echo "✅ Built web/js/jsonNodes.js"

# ─── Type Check ─────────────────────────────────────────
typecheck:
	@echo "═══ TypeScript type check ═══"
	"$(BUN)" x tsc --noEmit
	@echo "✅ Type check passed"

# ─── Lint ───────────────────────────────────────────────
lint:
	@echo "═══ Python lint (ruff) ═══"
	ruff check .
	@echo "═══ JS lint (biome) ═══"
	biome lint .
	@echo "✅ All lints passed"

# ─── Format ─────────────────────────────────────────────
format:
	@echo "═══ Format (ruff + biome) ═══"
	ruff format . && ruff check --fix .
	biome format --write .
	@echo "✅ Formatted"

# ─── Test ───────────────────────────────────────────────
test: build
	@echo "═══ Backend tests ═══"
	python -m unittest discover -s tests -p 'test_*unittest.py' -v
	@echo "═══ Frontend tests ═══"
	node tests/test_frontend_preview.mjs
	@echo "✅ All tests passed"

integration:
	@echo "═══ Integration tests ═══"
	python -m unittest tests.test_integration -v
	@echo "✅ Integration tests passed"

# ─── Compound targets ──────────────────────────────────
ci: build typecheck lint test integration
	@echo "✅ CI checks passed"

release: format ci
	@echo "🚀 Release complete"

# ─── Clean ──────────────────────────────────────────────
clean:
	@echo "Cleaning build artifacts..."
	@find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	@echo "✅ Clean"

# ─── Local overrides ───────────────────────────────────
# Include optional local targets from Makefile.local (gitignored)
-include Makefile.local
