VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
LDFLAGS := -s -w -X main.version=$(VERSION)
PLATFORMS := linux/amd64 linux/arm64 darwin/arm64 darwin/amd64 windows/amd64 windows/arm64

.PHONY: generate build run test e2e dist up down

generate:            ## regenerate Templ components
	go tool templ generate

build: generate      ## build ./bin/localchat for this machine
	go build -trimpath -ldflags "$(LDFLAGS)" -o bin/localchat ./cmd/localchat

run: build           ## run in the foreground (reads ./.env)
	./bin/localchat --debug

test:                ## unit + handler tests (no model needed)
	go test -race ./...

e2e:                 ## browser test against a running instance + real model
	uv run scripts/e2e.py

dist: generate       ## cross-compile for every platform into ./dist
	@for p in $(PLATFORMS); do \
		os=$${p%/*}; arch=$${p#*/}; ext=$$( [ $$os = windows ] && echo .exe ); \
		echo "  $$os/$$arch"; \
		CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags "$(LDFLAGS)" \
			-o dist/localchat-$$os-$$arch$$ext ./cmd/localchat || exit 1; \
	done

up:                  ## start app + llama.cpp + Gemma in containers
	docker compose up -d --build

down:
	docker compose down
