# ============================================================
# OCDP root orchestration Makefile
# ============================================================

SHELL := /bin/bash

COMPOSE_BIN ?= docker compose
ROOT_COMPOSE := docker-compose.yml
COMPOSE := $(COMPOSE_BIN) -f $(ROOT_COMPOSE)

.PHONY: help install up restart stop clean run-2 clean-2 docker-dev docker-prod docker-up docker-down docker-logs docker-ps test

.DEFAULT_GOAL := help

help:
	@echo ""
	@echo "OCDP commands"
	@echo "────────────────────────────────────────"
	@echo "  make install      Install local Go / frontend dependencies"
	@echo "  make up           Build and start the complete platform: DB + API + web gateway"
	@echo "  make restart      Restart the complete platform without removing volumes"
	@echo "  make stop         Stop containers, keep volumes"
	@echo "  make clean        Stop containers and remove project volumes"
	@echo "  make run-2        Alias of up, kept for old docs / muscle memory"
	@echo "  make docker-dev   Alias of up"
	@echo "  make docker-prod  Alias of up"
	@echo "  make docker-up    Alias of up"
	@echo "  make docker-down  Stop containers, keep volumes"
	@echo "  make clean-2      Stop containers and remove project volumes"
	@echo "  make docker-logs  Follow Compose logs"
	@echo "  make docker-ps    Show Compose service status"
	@echo "  make test         Run structured verification script"
	@echo ""
	@echo "Default local ports: web=18080, https=18443, backend=18081, postgres=15432"
	@echo "Override with WEB_HTTP_PORT / WEB_HTTPS_PORT / BACKEND_PORT / POSTGRES_PORT."
	@echo ""

install:
	@echo "→ Downloading backend modules"
	@cd backend && go mod download
	@echo "→ Installing frontend dependencies"
	@cd frontend && npm ci

up:
	@echo "→ Building and starting OCDP stack"
	@$(COMPOSE) up --build -d
	@echo ""
	@$(COMPOSE) ps -a
	@echo ""
	@echo "Web:     http://localhost:$${WEB_HTTP_PORT:-18080}"
	@echo "Backend: http://localhost:$${BACKEND_PORT:-18081}/health"

restart:
	@echo "→ Restarting OCDP stack"
	@$(COMPOSE) up --build -d --force-recreate
	@$(COMPOSE) ps -a

stop:
	@$(COMPOSE) down --remove-orphans

clean:
	@$(COMPOSE) down -v --remove-orphans

run-2: up

docker-dev: up

docker-prod: up

docker-up: up

docker-down:
	@$(MAKE) stop

clean-2:
	@$(MAKE) clean

docker-logs:
	@$(COMPOSE) logs -f

docker-ps:
	@$(COMPOSE) ps -a

test:
	@test/readme-deployment-refresh.sh
