# lf_mri_platform — unified MRI microservice stack
# Requires: Docker Desktop with Compose v2, GNU make
#
# Service targets:
#   make plug          — start all services in stub mode (no hardware)
#   make real          — start all services in real hardware mode
#   make down          — stop and remove containers
#   make build         — build all images without starting
#   make logs          — tail logs from all services
#   make health        — check all service health endpoints
#   make restart       — restart a single service: make restart svc=orchestrator
#   make ps            — show container status
#
# GUI targets:
#   make install       — install prerequisites (venv + deps for GUI)
#   make gui           — start GUI only (no Docker services)
#   make start         — start Docker services + GUI (plug mode)
#   make start-real    — start Docker services + GUI (real mode)
#
# Dev targets:
#   make shell         — open shell in a running container: make shell svc=orchestrator
#   make clean         — stop + remove volumes (full reset)

.PHONY: up down plug real logs health restart build ps \
        install gui start start-real shell clean

ENV_FILE  := .env
GUI_DIR   := apps/gui
VENV      := $(GUI_DIR)/.venv
PYTHON    := python

# ── Bootstrap ────────────────────────────────────────────────────────────────

$(ENV_FILE):
	@echo "Creating .env from .env.example..."
	cp .env.example $(ENV_FILE)

install: $(ENV_FILE)
	powershell -ExecutionPolicy Bypass -File install.ps1

# ── Service startup ──────────────────────────────────────────────────────────

up: $(ENV_FILE)
	docker compose --env-file $(ENV_FILE) up --build -d

plug: $(ENV_FILE)
	ORCHESTRATOR_MODE=plug docker compose --env-file $(ENV_FILE) up --build -d

real: $(ENV_FILE)
	ORCHESTRATOR_MODE=real docker compose --env-file $(ENV_FILE) up --build -d

down:
	docker compose down

clean:
	docker compose down --volumes

build: $(ENV_FILE)
	docker compose --env-file $(ENV_FILE) build

# ── GUI ──────────────────────────────────────────────────────────────────────

gui:
	$(VENV)/Scripts/python $(GUI_DIR)/app.py

start: plug
	$(MAKE) gui

start-real: real
	$(MAKE) gui

# ── Monitoring ───────────────────────────────────────────────────────────────

logs:
	docker compose logs -f

ps:
	docker compose ps

restart:
	docker compose restart $(svc)

shell:
	docker compose exec $(svc) /bin/sh

# ── Health checks ────────────────────────────────────────────────────────────

health:
	@echo "orchestrator  :" && curl -sf http://localhost:$${ORCHESTRATOR_PORT:-1717}/health  || echo "OFFLINE"
	@echo "seq-interp    :" && curl -sf http://localhost:$${SEQ_INTERP_PORT:-7475}/health    || echo "OFFLINE"
	@echo "spectrometer  :" && curl -sf http://localhost:$${SPECTROMETER_PORT:-8000}/api/    || echo "OFFLINE"
	@echo "reconstructor :" && curl -sf http://localhost:$${RECONSTRUCTOR_PORT:-8081}/health || echo "OFFLINE"
	@echo "spectroscopy  :" && curl -sf http://localhost:$${SPECTROSCOPY_PORT:-8002}/health  || echo "OFFLINE"
