.DEFAULT_GOAL := help
COMPOSE := docker compose
COMPOSE_FILE := docker-compose.yml

.PHONY: help setup build up down restart logs logs-app logs-nginx shell

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

setup: ## First-run: copy .env.example → .env (skips if .env exists), then remind to set secrets
	@if [ ! -f .env ]; then \
		cp .env.example .env; \
		echo "Created .env from .env.example."; \
		echo "Now set JWT_SECRET and COLLECT_SALT:"; \
		echo "  node -e \"console.log(require('crypto').randomBytes(48).toString('hex'))\"  # JWT_SECRET"; \
		echo "  node -e \"console.log(require('crypto').randomBytes(32).toString('hex'))\"  # COLLECT_SALT"; \
	else \
		echo ".env already exists, skipping"; \
	fi

build: ## Build the image
	$(COMPOSE) -f $(COMPOSE_FILE) build

up: ## Start the stack in the background
	$(COMPOSE) -f $(COMPOSE_FILE) up -d

down: ## Stop and remove containers
	$(COMPOSE) -f $(COMPOSE_FILE) down

restart: ## Restart the stack
	$(COMPOSE) -f $(COMPOSE_FILE) restart

logs: ## Tail logs (Ctrl+C to stop)
	$(COMPOSE) -f $(COMPOSE_FILE) logs -f

shell: ## Open a shell in the app container
	$(COMPOSE) -f $(COMPOSE_FILE) exec app sh

logs-app: ## Tail only the Node app logs
	$(COMPOSE) -f $(COMPOSE_FILE) logs -f app

logs-nginx: ## Tail only the nginx logs
	$(COMPOSE) -f $(COMPOSE_FILE) logs -f nginx
