# ------------------------------------------------------------------------------
# Makefile for go-cart-actor
#
# Key targets:
#   make protogen      - Generate protobuf + gRPC code into proto/
#   make clean_proto   - Remove generated proto *.pb.go files
#   make verify_proto  - Ensure no stray root-level *.pb.go files exist
#   make build         - Build the project
#   make test          - Run tests (verbose)
#   make tidy          - Run go mod tidy
#   make regen         - Clean proto, regenerate, tidy, verify, build
#   make help          - Show this help
#
# Conventions:
#   - All .proto files live in $(PROTO_DIR)
#   - Generated Go code is emitted under $(PROTO_DIR) via go_package mapping
#   - go_package is set to: git.tornberg.me/go-cart-actor/proto;messages
# ------------------------------------------------------------------------------

MODULE_PATH := git.tornberg.me/go-cart-actor
PROTO_DIR   := proto
PROTOS      := $(PROTO_DIR)/messages.proto $(PROTO_DIR)/control_plane.proto

# Allow override: make PROTOC=/path/to/protoc
PROTOC ?= protoc

# Tools (auto-detect; can override)
PROTOC_GEN_GO        ?= $(shell command -v protoc-gen-go 2>/dev/null)
PROTOC_GEN_GO_GRPC   ?= $(shell command -v protoc-gen-go-grpc 2>/dev/null)

GO        ?= go

# Colors (optional)
GREEN  := \033[32m
RED    := \033[31m
YELLOW := \033[33m
RESET  := \033[0m

# ------------------------------------------------------------------------------

.PHONY: protogen clean_proto verify_proto tidy build test regen help check_tools

help:
	@echo "Available targets:"
	@echo "  protogen      Generate protobuf & gRPC code"
	@echo "  clean_proto   Remove generated *.pb.go files in $(PROTO_DIR)"
	@echo "  verify_proto  Ensure no root-level *.pb.go files (old layout)"

	@echo "  tidy          Run go mod tidy"
	@echo "  build         Build the module"
	@echo "  test          Run tests (verbose)"
	@echo "  regen         Clean proto, regenerate, tidy, verify, and build"
	@echo "  check_tools   Verify protoc + plugins are installed"

check_tools:
	@if [ -z "$(PROTOC_GEN_GO)" ] || [ -z "$(PROTOC_GEN_GO_GRPC)" ]; then \
		echo "$(RED)Missing protoc-gen-go or protoc-gen-go-grpc in PATH.$(RESET)"; \
		echo "Install with:"; \
		echo "  go install google.golang.org/protobuf/cmd/protoc-gen-go@latest"; \
		echo "  go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest"; \
		exit 1; \
	fi
	@if ! command -v "$(PROTOC)" >/dev/null 2>&1; then \
		echo "$(RED)protoc not found. Install protoc (e.g. via package manager)$(RESET)"; \
		exit 1; \
	fi
	@echo "$(GREEN)All required tools detected.$(RESET)"

protogen: check_tools
	@echo "$(YELLOW)Generating protobuf code (outputs -> ./proto)...$(RESET)"
	$(PROTOC) -I $(PROTO_DIR) \
	  --go_out=./pkg/messages --go_opt=paths=source_relative \
	  --go-grpc_out=./pkg/messages --go-grpc_opt=paths=source_relative \
	  $(PROTOS)
	@echo "$(GREEN)Protobuf generation complete.$(RESET)"

clean_proto:
	@echo "$(YELLOW)Removing generated protobuf files...$(RESET)"
	@rm -f $(PROTO_DIR)/*_grpc.pb.go $(PROTO_DIR)/*.pb.go
	@rm -f *.pb.go
	@rm -rf git.tornberg.me
	@echo "$(GREEN)Clean complete.$(RESET)"

verify_proto:
	@echo "$(YELLOW)Verifying proto layout...$(RESET)"
	@if ls *.pb.go >/dev/null 2>&1; then \
		echo "$(RED)ERROR: Found root-level generated *.pb.go files (should be only under $(PROTO_DIR)/).$(RESET)"; \
		ls -1 *.pb.go; \
		exit 1; \
	fi
	@echo "$(GREEN)Proto layout OK (no root-level *.pb.go files).$(RESET)"













tidy:
	@echo "$(YELLOW)Running go mod tidy...$(RESET)"
	$(GO) mod tidy
	@echo "$(GREEN)tidy complete.$(RESET)"

build:
	@echo "$(YELLOW)Building...$(RESET)"
	$(GO) build ./...
	@echo "$(GREEN)Build success.$(RESET)"

test:
	@echo "$(YELLOW)Running tests...$(RESET)"
	$(GO) test -v ./...
	@echo "$(GREEN)Tests completed.$(RESET)"

regen: clean_proto protogen tidy verify_proto build
	@echo "$(GREEN)Full regenerate cycle complete.$(RESET)"

# Utility: show proto sources and generated outputs
print_proto:
	@echo "Proto sources:"
	@ls -1 $(PROTOS)
	@echo ""
	@echo "Generated files (if any):"
	@ls -1 $(PROTO_DIR)/*pb.go 2>/dev/null || echo "(none)"

# Prevent make from treating these as file targets if similarly named files appear.
.SILENT: help check_tools protogen clean_proto verify_proto tidy build test regen print_proto
