BIN     := bin
TAGS    := -tags goolm
LDFLAGS := -ldflags="-s -w"

.PHONY: build build-launcher build-agentctl build-register \
        test ci \
        list start stop remove register \
        clean tidy

# ── Test ───────────────────────────────────────────────────────────────────

test:
	go test $(TAGS) ./...

# ── Build ──────────────────────────────────────────────────────────────────

ci: test build

build: build-launcher build-agentctl build-register

build-launcher:
	@mkdir -p $(BIN)
	go build $(TAGS) $(LDFLAGS) -o $(BIN)/launcher ./cmd/launcher

build-agentctl:
	@mkdir -p $(BIN)
	go build $(TAGS) $(LDFLAGS) -o $(BIN)/agentctl ./cmd/agentctl

build-register:
	@mkdir -p $(BIN)
	go build $(TAGS) $(LDFLAGS) -o $(BIN)/register ./cmd/register

# ── Agent management (shortcuts via agentctl) ──────────────────────────────

list:
	@go run ./cmd/agentctl list

start:
	@go run ./cmd/agentctl start $(AGENT)

stop:
	@go run ./cmd/agentctl stop $(AGENT)

remove:
	@go run ./cmd/agentctl remove $(AGENT)

# Usage: make register USERNAME=assistant-bot DISPLAYNAME="Assistant" ENV_VAR=MATRIX_TOKEN_ASSISTANT
register:
	MATRIX_ADMIN_TOKEN=$$MATRIX_ADMIN_TOKEN \
	go run ./cmd/register \
		--homeserver $$MATRIX_HOMESERVER \
		--username $(USERNAME) \
		--displayname "$(DISPLAYNAME)" \
		--env-var $(ENV_VAR)

# ── Dev ────────────────────────────────────────────────────────────────────

tidy:
	go mod tidy

clean:
	rm -rf $(BIN) run/
