# DESI Explorer — root Makefile
#
# Orchestrates the Odin renderer (src/) and the Go/Pulumi infrastructure
# (infra/). Intended to be called directly by Gitea Actions CI.

ODIN         ?= odin
CARGO        ?= cargo
BIN          ?= bin
BINARY       := $(BIN)/desi_explorer
ODIN_FLAGS   := -collection:lib=lib/local
WASM_DEFINE  := RAYLIB_WASM_LIB=env.o

.PHONY: help setup run build build-debug build-web test clean fmt \
        api-run api-build api-test api-check api-fmt \
        infra-preview infra-up infra-down infra-refresh infra-static \
        renovate-validate

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

## ---- Setup ---------------------------------------------------------------

setup: ## Pull submodules + tidy Go deps
	@git submodule update --init --recursive
	@mkdir -p lib/local
	@cd infra && go mod tidy

## ---- Renderer (Odin) -----------------------------------------------------

run: ## Run the native app
	@mkdir -p lib/local
	$(ODIN) run src $(ODIN_FLAGS)

build: ## Release build -> bin/desi_explorer
	@mkdir -p lib/local $(BIN)
	$(ODIN) build src $(ODIN_FLAGS) -o:speed -out:$(BINARY)

build-debug: ## Debug build -> bin/desi_explorer
	@mkdir -p lib/local $(BIN)
	$(ODIN) build src $(ODIN_FLAGS) -o:none -debug -out:$(BINARY)

build-web: ## WebAssembly build -> build/web (needs emscripten)
	@scripts/build_web.sh

test: ## Run tests (Odin src/ + Go infra/ + Rust api/)
	@mkdir -p lib/local
	$(ODIN) test src $(ODIN_FLAGS)
	@cd infra && go test ./...
	@cd api && $(CARGO) test

clean: ## Remove build artifacts
	rm -rf $(BIN) build
	@cd api && $(CARGO) clean

fmt: ## Format Odin sources with odinfmt
	odinfmt src

## ---- API (Rust + axum) ---------------------------------------------------

api-run: ## Run the API server (dev)
	@cd api && $(CARGO) run

api-build: ## Release build the API -> api/target/release/
	@cd api && $(CARGO) build --release

api-test: ## Run API unit/integration tests
	@cd api && $(CARGO) test

api-check: ## Typecheck + format-check + lint the API
	@cd api && $(CARGO) fmt --all --check && $(CARGO) clippy --all-targets -- -D warnings

api-fmt: ## Format Rust sources with rustfmt
	@cd api && $(CARGO) fmt

## ---- Infrastructure (Go + Pulumi) ----------------------------------------

infra-preview: ## Preview infra changes against the cluster
	@pulumi preview --cwd infra

infra-up: ## Deploy/update infra
	@pulumi up --cwd infra

infra-down: ## Tear down the stack's resources
	@pulumi destroy --cwd infra

infra-refresh: ## Refresh Pulumi state against the live cluster
	@pulumi refresh --cwd infra

infra-static: ## Typecheck + vet the Go infra without Pulumi
	@cd infra && go build -o /dev/null . && go vet ./...

## ---- Tooling -------------------------------------------------------------

renovate-validate: ## Validate renovate.json
	bunx --package renovate renovate-config-validator renovate.json --strict
