.PHONY: check lint test test-unit test-integration build web-build web-lint \
       docker-build docker-up docker-down smoke-test clean install-hooks

# Output directory for built binaries
BIN_DIR := bin

# All cmd/ binaries to build
CMDS := lumina-server lumina-cli lumina-keygen lumina-dbcheck reset-sandboxes

# --- Local CI (run before committing) ---

check: lint test build web-build web-lint
	@echo "All checks passed."

# --- Go ---

lint:
	go vet ./...
	@test -z "$$(gofmt -l .)" || { echo "Files need formatting:"; gofmt -l .; exit 1; }

test:
	go test -race -count=1 ./...

test-unit:
	go test -short ./...

test-integration:
	go test -run Integration ./...

build: $(addprefix $(BIN_DIR)/,$(CMDS))

$(BIN_DIR)/%: cmd/%
	@mkdir -p $(BIN_DIR)
	go build -o $@ ./cmd/$*

# --- Frontend ---

web-build:
	cd web && npm ci && npm run build

web-lint:
	cd web && npx tsc --noEmit

# --- Docker ---

docker-build:
	docker build -f Dockerfile.server -t lumina-server:latest .
	docker build -f Dockerfile.sandbox -t lumina-sandbox:latest .

docker-up:
	docker compose up -d

docker-down:
	docker compose down

# --- Smoke test ---

smoke-test:
	./scripts/smoke-test.sh

# --- Git hooks ---

install-hooks:
	cp scripts/pre-commit "$$(git rev-parse --git-common-dir)/hooks/pre-commit"
	@echo "Pre-commit hook installed."

# --- Cleanup ---

clean:
	rm -rf $(BIN_DIR)
