# Load GATEWAY_* config from .env (if present) into the environment for recipes.
# Real environment variables already set take precedence over the file.
-include .env
export

BINARY    := bin/gateway
DB_NAME   := lmr-db
DB_PORT   := 5432
DB_USER   := gateway
DB_PASS   := gateway

.DEFAULT_GOAL := help
.PHONY: help dev dev-db dev-stop run build ui migrate-up migrate-down test lint tidy clean

help: ## List available targets
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) \
		| awk 'BEGIN{FS=":.*?## "}{printf "  \033[36m%-14s\033[0m %s\n", $$1, $$2}'

# ---------------------------------------------------------------------------
# Development
# ---------------------------------------------------------------------------

dev: .env ## Start Postgres, run the Go server, and the Vite dev server in parallel
	@# Ensure the .env has non-empty GATEWAY_SECURITY_ENCRYPTION_KEY
	@grep -q 'GATEWAY_SECURITY_ENCRYPTION_KEY=.\+' .env || \
		{ echo ""; \
		  echo "  ERROR: GATEWAY_SECURITY_ENCRYPTION_KEY is empty in .env"; \
		  echo "  Run: echo GATEWAY_SECURITY_ENCRYPTION_KEY=\$$(openssl rand -hex 32) >> .env"; \
		  echo ""; exit 1; }
	@$(MAKE) --no-print-directory dev-db
	@# Run Go server and Vite dev server side-by-side, kill both on Ctrl-C
	@trap 'kill 0' INT; \
		go run ./cmd/server & \
		(cd dashboard && npm run dev) & \
		wait

dev-db: ## Start (or create) the dev Postgres container and wait until it is ready
	@# Start Postgres container if not already running
	@if ! docker ps --format '{{.Names}}' | grep -q '^$(DB_NAME)$$'; then \
		if docker ps -a --format '{{.Names}}' | grep -q '^$(DB_NAME)$$'; then \
			echo "→ starting existing postgres container $(DB_NAME)"; \
			docker start $(DB_NAME); \
		else \
			echo "→ creating postgres container $(DB_NAME)"; \
			docker run -d --name $(DB_NAME) \
				-e POSTGRES_USER=$(DB_USER) \
				-e POSTGRES_PASSWORD=$(DB_PASS) \
				-e POSTGRES_DB=$(DB_USER) \
				-p $(DB_PORT):5432 \
				postgres:16-alpine; \
		fi; \
	else \
		echo "→ postgres $(DB_NAME) already running"; \
	fi
	@# Wait until Postgres accepts connections
	@echo "→ waiting for postgres…"; \
	until docker exec $(DB_NAME) pg_isready -U $(DB_USER) -q 2>/dev/null; do sleep 1; done; \
	echo "→ postgres ready"

dev-stop: ## Stop and remove the dev Postgres container
	docker rm -f $(DB_NAME) 2>/dev/null && echo "→ removed $(DB_NAME)" || echo "→ $(DB_NAME) not found"

.env:
	@echo ""; \
	 echo "  ERROR: .env not found. Copy the example and fill in the required fields:"; \
	 echo ""; \
	 echo "    cp .env.example .env"; \
	 echo "    echo GATEWAY_SECURITY_ENCRYPTION_KEY=\$$(openssl rand -hex 32) >> .env"; \
	 echo ""; exit 1

run: ## Run the gateway (migrations applied automatically on startup)
	go run ./cmd/server

build: ui ## Build the dashboard, then compile the server binary
	go build -o $(BINARY) ./cmd/server

ui: ## Build the dashboard into the embedded asset dir
	cd dashboard && npm run build

migrate-up: ## Apply all pending migrations
	go run ./cmd/migrate up

migrate-down: ## Roll back the most recent migration
	go run ./cmd/migrate down

test: ## Run Go tests
	go test ./...

lint: ## Run golangci-lint
	golangci-lint run

tidy: ## Tidy go.mod / go.sum
	go mod tidy

clean: ## Remove build artifacts (keeps the embed placeholder)
	rm -rf $(BINARY)
	find internal/web/dist -mindepth 1 ! -name .gitkeep -delete 2>/dev/null || true
