# ------------------------------------------------------------------------------
# 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.k6n.net/mats/go-cart-actor/proto;messages
# ------------------------------------------------------------------------------

MODULE_PATH := git.k6n.net/mats/go-cart-actor
PROTO_DIR   := proto
PROTOS      := $(PROTO_DIR)/cart.proto $(PROTO_DIR)/control_plane.proto $(PROTO_DIR)/checkout.proto $(PROTO_DIR)/order.proto $(PROTO_DIR)/profile.proto
CART_PROTO_DIR := $(PROTO_DIR)/cart
CONTROL_PROTO_DIR := $(PROTO_DIR)/control
CHECKOUT_PROTO_DIR := $(PROTO_DIR)/checkout
ORDER_PROTO_DIR := $(PROTO_DIR)/order
PROFILE_PROTO_DIR := $(PROTO_DIR)/profile

# 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=./proto/cart --go_opt=paths=source_relative \
	  --go-grpc_out=./proto/cart --go-grpc_opt=paths=source_relative \
	  $(PROTO_DIR)/cart.proto
	$(PROTOC) -I $(PROTO_DIR) \
	  --go_out=./proto/control --go_opt=paths=source_relative \
	  --go-grpc_out=./proto/control --go-grpc_opt=paths=source_relative \
	  $(PROTO_DIR)/control_plane.proto
	$(PROTOC) -I $(PROTO_DIR) \
	  --go_out=./proto/checkout --go_opt=paths=source_relative \
	  --go-grpc_out=./proto/checkout --go-grpc_opt=paths=source_relative \
	  $(PROTO_DIR)/checkout.proto
	$(PROTOC) -I $(PROTO_DIR) \
	  --go_out=./proto/order --go_opt=paths=source_relative \
	  --go-grpc_out=./proto/order --go-grpc_opt=paths=source_relative \
	  $(PROTO_DIR)/order.proto
	$(PROTOC) -I $(PROTO_DIR) \
	  --go_out=./proto/profile --go_opt=paths=source_relative \
	  $(PROTO_DIR)/profile.proto
	@echo "$(GREEN)Protobuf generation complete.$(RESET)"

clean_proto:
	@echo "$(YELLOW)Removing generated protobuf files...$(RESET)"
	@rm -f $(PROTO_DIR)/cart/*_grpc.pb.go $(PROTO_DIR)/cart/*.pb.go
	@rm -f $(PROTO_DIR)/control/*_grpc.pb.go $(PROTO_DIR)/control/*.pb.go
	@rm -f $(PROTO_DIR)/checkout/*_grpc.pb.go $(PROTO_DIR)/checkout/*.pb.go
	@rm -f $(PROTO_DIR)/order/*_grpc.pb.go $(PROTO_DIR)/order/*.pb.go
	@rm -f $(PROTO_DIR)/profile/*.pb.go
	@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)/ subdirs).$(RESET)"; \
		ls -1 *.pb.go; \
		exit 1; \
	fi
	@echo "$(GREEN)Proto layout OK (no root-level *.pb.go files).$(RESET)"

run:
	REDIS_ADDRESS=10.10.3.18:6379 \
	REDIS_PASSWORD=slaskredis \
	CART_DEBUG_PORT=8091 \
	CART_PORT=8090 \
	go run ./cmd/cart/

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
