.PHONY: install dev dev-server dev-client dev-client-cahoo ensure-env build start stop restart rebuild logs start-local start-server start-client start-client-cahoo clean help

# Ports
SERVER_PORT      ?= 3008
CLIENT_PORT      ?= 3009
CLIENT_CAHOO_PORT ?= 3010
IMAGE_NAME       ?= booking-proxy
CONTAINER        ?= booking-proxy

# Public-facing URLs baked into the Next.js builds
API_URL ?= https://cahootravel.com
WS_URL  ?= wss://cahootravel.com

# --- Install ---

install: ## Install dependencies for local dev
	npm ci
	cd client && npm ci
	cd client-cahoo && npm ci

# --- Development ---

dev: ## Run server, client, and client-cahoo in dev mode (local)
	@echo "Starting server on port $(SERVER_PORT), client on port $(CLIENT_PORT), client-cahoo on port $(CLIENT_CAHOO_PORT)..."
	PORT=$(SERVER_PORT) npx tsx watch src/index.ts & \
	cd client && NEXT_PUBLIC_API_URL=http://localhost:$(SERVER_PORT) NEXT_PUBLIC_WS_URL=ws://localhost:$(SERVER_PORT) npx next dev -p $(CLIENT_PORT) & \
	cd client-cahoo && NEXT_PUBLIC_API_URL=http://localhost:$(SERVER_PORT) NEXT_PUBLIC_WS_URL=ws://localhost:$(SERVER_PORT) npx next dev -p $(CLIENT_CAHOO_PORT) & \
	wait

dev-server: ## Run only the backend in dev mode (local)
	PORT=$(SERVER_PORT) npx tsx watch src/index.ts

dev-client: ## Run only the original frontend in dev mode (local)
	cd client && NEXT_PUBLIC_API_URL=http://localhost:$(SERVER_PORT) NEXT_PUBLIC_WS_URL=ws://localhost:$(SERVER_PORT) npx next dev -p $(CLIENT_PORT)

dev-client-cahoo: ## Run only client-cahoo in dev mode (local)
	cd client-cahoo && NEXT_PUBLIC_API_URL=http://localhost:$(SERVER_PORT) NEXT_PUBLIC_WS_URL=ws://localhost:$(SERVER_PORT) npx next dev -p $(CLIENT_CAHOO_PORT)

# --- Docker (production) ---

ensure-env: ## Generate .env if missing
	@test -f .env || ./setup-env.sh

build: ensure-env ## Build Docker image
	docker build \
		--build-arg NEXT_PUBLIC_API_URL=$(API_URL) \
		--build-arg NEXT_PUBLIC_WS_URL=$(WS_URL) \
		-t $(IMAGE_NAME) .

start: ensure-env ## Start Docker container (builds image if missing)
	@docker image inspect $(IMAGE_NAME) >/dev/null 2>&1 || $(MAKE) build
	docker run -d --name $(CONTAINER) \
		-p $(SERVER_PORT):3008 \
		-p $(CLIENT_PORT):3009 \
		-p $(CLIENT_CAHOO_PORT):3010 \
		--restart unless-stopped \
		$(if $(wildcard .env),--env-file .env,) \
		$(IMAGE_NAME)
	@echo "Server: http://localhost:$(SERVER_PORT)  Client: http://localhost:$(CLIENT_PORT)  Cahoo: http://localhost:$(CLIENT_CAHOO_PORT)"

stop: ## Stop and remove Docker container
	-docker stop $(CONTAINER) 2>/dev/null
	-docker rm $(CONTAINER) 2>/dev/null

restart: stop start ## Restart Docker container

rebuild: ensure-env build stop start ## Rebuild image and restart container

logs: ## Tail Docker container logs
	docker logs -f $(CONTAINER)

# --- Local production (no Docker) ---

build-local: ## Build server, client, and client-cahoo locally
	npm run build
	cd client && NEXT_PUBLIC_API_URL=http://localhost:$(SERVER_PORT) NEXT_PUBLIC_WS_URL=ws://localhost:$(SERVER_PORT) npm run build
	cd client-cahoo && NEXT_PUBLIC_API_URL=http://localhost:$(SERVER_PORT) NEXT_PUBLIC_WS_URL=ws://localhost:$(SERVER_PORT) npm run build

start-local: ## Start server, client, and client-cahoo locally (production)
	@echo "Starting server on port $(SERVER_PORT), client on port $(CLIENT_PORT), client-cahoo on port $(CLIENT_CAHOO_PORT)..."
	PORT=$(SERVER_PORT) node dist/index.js & \
	cd client && PORT=$(CLIENT_PORT) npx next start -p $(CLIENT_PORT) & \
	cd client-cahoo && PORT=$(CLIENT_CAHOO_PORT) npx next start -p $(CLIENT_CAHOO_PORT) & \
	wait

start-server: ## Start only the backend locally (production)
	PORT=$(SERVER_PORT) node dist/index.js

start-client: ## Start only the original frontend locally (production)
	cd client && PORT=$(CLIENT_PORT) npx next start -p $(CLIENT_PORT)

start-client-cahoo: ## Start only client-cahoo locally (production)
	cd client-cahoo && PORT=$(CLIENT_CAHOO_PORT) npx next start -p $(CLIENT_CAHOO_PORT)

# --- Utilities ---

clean: ## Remove build artifacts
	rm -rf dist
	rm -rf client/.next
	rm -rf client-cahoo/.next

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