# Makefile for Go Places Project
# ---------------------------------
# Port Configuration:
#   - Go API:      defaults to :8080 (via ADDR env var)
#   - React UI:    defaults to :5173 (via Vite)
#   - Proxy:       forwards /api, /graphql, /media, /healthz to Go API
# ---------------------------------

# --- Configuration ---
API_CHART_PATH := ./charts/api
LOOSE_CHART_PATH := ./charts/go-loose
API_DIR := api
BIN_DIR := bin
API_BINARY := $(BIN_DIR)/go-places

# --- Targets ---

.PHONY: all build build-api build-loose run test fmt vet validate dry-run-deploy deploy clean help

# Default target: build the service that lives in this repository.
all: build-api

# Main build target.
build: build-api

# Build go-places. The module root is api/, so the build has to run there
# rather than from the repository root.
build-api:
	@echo "--- Building go-places ---"
	@mkdir -p $(BIN_DIR)
	cd $(API_DIR) && go build -o ../$(API_BINARY) ./cmd
	@echo "built $(API_BINARY)"

# go-loose is the authentication service this API depends on. Its source lives in
# its own repository, so it cannot be built from here.
build-loose:
	@echo "go-loose is a separate service and its source is not part of this repository." >&2
	@echo "See charts/$(notdir $(LOOSE_CHART_PATH))/values.yaml for how it is deployed." >&2
	@exit 1

# Run the service locally with development defaults.
# The already-built binary is exec'd rather than run through "go run", so the
# process make starts is the service itself: startup is immediate (no second
# compile) and Ctrl-C reaches it directly.
# Environment variables:
#   ADDR      - Go API listen address (default: :8080)
#   AUTH_MODE   - Authentication mode: dev or jwt (default: dev)
#   ENABLE_REST   - Enable REST endpoints (default: true)
run: build-api
	cd $(API_DIR) && AUTH_MODE=dev ENABLE_REST=true exec ../$(API_BINARY)

# React UI development server
# Forwards /api, /graphql, /media, /healthz to the Go API via Vite proxy.
# Vite dev server runs on :5173 by default (or next available port).
# Environment variable:
#   VITE_PROXY_TARGET - API target URL (default: http://localhost:8080)
web-dev:
	cd web && npm run dev

test:
	cd $(API_DIR) && go test ./...

fmt:
	cd $(API_DIR) && go fmt ./...

vet:
	cd $(API_DIR) && go vet ./...

# Validate all Helm charts.
validate:
	@echo "--- Validating Helm Charts ---"
	helm lint $(API_CHART_PATH) $(LOOSE_CHART_PATH)
	@echo "Helm linting complete."

# Dry run deployment to validate manifests before applying to a cluster.
dry-run-deploy:
	@echo "--- DRY RUN: Validating API Helm Chart ---"
	helm lint $(API_CHART_PATH)
	@echo "--- DRY RUN: Validating go-loose Helm Chart ---"
	helm lint $(LOOSE_CHART_PATH)
	@echo "Dry run validation complete. Manifests look correct."

# Deploy all charts to a target environment (requires configured k8s context).
deploy: validate
	@echo "--- Deploying all charts to Kubernetes ---"
	helm upgrade --install go-places $(API_CHART_PATH) --namespace dev --create-namespace -f $(API_CHART_PATH)/values.yaml
	helm upgrade --install go-loose $(LOOSE_CHART_PATH) --namespace dev --create-namespace -f $(LOOSE_CHART_PATH)/values.yaml
	@echo "Deployment process initiated. Check cluster status."

# Clean up generated files or images (if any)
clean:
	@echo "--- Cleaning up build artifacts ---"
	rm -rf $(BIN_DIR)

# Help target
help:
	@echo "=========================================="
	@echo "Go Places Project Makefile Help"
	@echo "make all             : Build the go-places service."
	@echo "make build           : Alias for 'make all'."
	@echo "make build-api       : Build bin/go-places from ./api."
	@echo "make run             : Run the API locally with AUTH_MODE=dev."
	@echo "make web-dev         : Start Vite dev server for React UI (ports: API:8080, UI:5173)"
	@echo "make test            : Run the Go tests."
	@echo "make fmt / vet       : Format / vet the Go sources."
	@echo "make build-loose     : Not buildable here; go-loose is a separate repo."
	@echo "make validate        : Run 'helm lint' on both charts."
	@echo "make dry-run-deploy  : Validate Helm charts without deploying."
	@echo "make deploy          : Validate charts and deploy to Kubernetes."
	@echo "make clean           : Remove local build artifacts."
