# x0gp PoC server Makefile
#
# Common targets:
#   make tidy      - go mod tidy
#   make build     - compile poc-server (linux/arm64 by default for Orange Pi 5 Pro)
#   make run       - build + run locally (dev mode, port 8080)
#   make proto     - regenerate Go from proto/ via Docker
#   make test      - go test ./...
#   make lint      - go vet + (optional) golangci-lint
#   make docker    - build the api-gateway image
#   make compose   - run dev stack (postgres + redis + prometheus + grafana + api-gateway)

GO ?= go
APP := poc-server
BIN_DIR := bin
GOOS ?= linux
GOARCH ?= arm64
GOARM ?=
LDFLAGS := -s -w -X main.version=$(shell git describe --tags --always --dirty 2>/dev/null || echo dev)

# Honour explicit user overrides of GOOS/GOARCH (e.g. make build GOARCH=amd64).
ifeq ($(GOARCH),arm64)
  ifeq ($(GOOS),linux)
    TARGET := $(BIN_DIR)/$(APP)-linux-arm64
  endif
endif
ifeq ($(GOARCH),amd64)
  ifeq ($(GOOS),linux)
    TARGET := $(BIN_DIR)/$(APP)-linux-amd64
  endif
endif
ifeq ($(GOARCH),arm64)
  ifeq ($(GOOS),darwin)
    TARGET := $(BIN_DIR)/$(APP)-darwin-arm64
  endif
endif
ifeq ($(GOOS),windows)
  TARGET := $(BIN_DIR)/$(APP)-windows-$(GOARCH).exe
endif
TARGET ?= $(BIN_DIR)/$(APP)

.PHONY: tidy
tidy:
	$(GO) mod tidy

.PHONY: build
build:
	mkdir -p $(BIN_DIR)
	GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build -ldflags "$(LDFLAGS)" -o $(TARGET) ./cmd/poc-server
	@echo "built: $(TARGET)"

.PHONY: run
run: build
	X0GP_DEV_MODE=true X0GP_LOG_LEVEL=debug ./$(TARGET)

.PHONY: proto
proto:
	@echo "Regenerating protobuf Go code via buf in Docker..."
	docker run --rm -v "$$PWD":/workspace -w /workspace bufbuild/buf:latest generate
	@echo "Done. Generated code is in proto/gen/."

.PHONY: test
test:
	$(GO) test ./... -race -count=1

.PHONY: lint
lint:
	$(GO) vet ./...
	@command -v golangci-lint >/dev/null && golangci-lint run || echo "(golangci-lint not installed, skipping)"

.PHONY: docker
docker:
	docker build -t x0gp/api-gateway:dev -f Dockerfile .

.PHONY: compose-up
compose-up:
	docker compose up -d

.PHONY: compose-down
compose-down:
	docker compose down

.PHONY: clean
clean:
	rm -rf $(BIN_DIR)

.DEFAULT_GOAL := build
