# Status Dashboard Makefile
# Server runs in Docker, collectors run on monitored hosts

.PHONY: help build up down logs status clean

# Configuration
COMPOSE := docker compose
COLLECTOR_DIR := agent

help:
	@echo "Status Dashboard Management"
	@echo ""
	@echo "Server (Docker on apricot):"
	@echo "  make build         - Build server Docker image"
	@echo "  make up            - Start server"
	@echo "  make down          - Stop server"
	@echo "  make logs          - Tail server logs"
	@echo "  make status        - Check server health"
	@echo "  make restart       - Restart server"
	@echo ""
	@echo "Collectors (deploy to monitored hosts):"
	@echo "  make deploy HOSTS=platform-vps     - Deploy to one host"
	@echo "  make deploy HOSTS=all              - Deploy to all hosts"
	@echo "  make deploy-status                 - Check collectors on all hosts"
	@echo "  make deploy-logs HOST=platform-vps - Tail logs from host"
	@echo ""
	@echo "Available hosts: platform-vps, vpn-gateway, apricot, black"
	@echo ""
	@echo "Setup:"
	@echo "  make setup         - Initial setup (create .env, directories)"
	@echo "  make certs         - Generate mTLS certificates"
	@echo ""

# ============================================================
# SERVER (Docker)
# ============================================================

build:
	@echo "Building status-dashboard server..."
	$(COMPOSE) build

up:
	@echo "Starting status-dashboard server..."
	$(COMPOSE) up -d
	@echo "Server started. View logs: make logs"

down:
	@echo "Stopping status-dashboard server..."
	$(COMPOSE) down

restart:
	@echo "Restarting status-dashboard server..."
	$(COMPOSE) restart

logs:
	$(COMPOSE) logs -f

status:
	@echo "=== Server Status ==="
	@$(COMPOSE) ps
	@echo ""
	@curl -sf http://localhost:5000/health && echo "Health: OK" || echo "Health: FAILED"

# ============================================================
# COLLECTORS (deploy to monitored hosts)
# ============================================================

# Build collector
collector-build:
	@echo "Building metrics collector..."
	cd $(COLLECTOR_DIR) && npm run build

# Deploy to hosts
# Usage: make deploy HOSTS=platform-vps
#        make deploy HOSTS=all
deploy: collector-build
	@if [ "$(HOSTS)" = "all" ]; then \
		echo "Deploying to all hosts..."; \
		cd $(COLLECTOR_DIR) && ./deploy.sh platform-vps; \
		cd $(COLLECTOR_DIR) && ./deploy.sh vpn-gateway; \
		cd $(COLLECTOR_DIR) && ./deploy.sh apricot; \
		cd $(COLLECTOR_DIR) && ./deploy.sh black; \
	elif [ -n "$(HOSTS)" ]; then \
		echo "Deploying to $(HOSTS)..."; \
		cd $(COLLECTOR_DIR) && ./deploy.sh $(HOSTS); \
	else \
		echo "Usage: make deploy HOSTS=<hostname|all>"; \
		echo "Hosts: platform-vps, vpn-gateway, apricot, black, all"; \
		exit 1; \
	fi

# Check collector status on all hosts
deploy-status:
	cd $(COLLECTOR_DIR) && make status

# Tail logs from a specific host
# Usage: make deploy-logs HOST=platform-vps
deploy-logs:
	@if [ -z "$(HOST)" ]; then \
		echo "Usage: make deploy-logs HOST=<hostname>"; \
		exit 1; \
	fi
	cd $(COLLECTOR_DIR) && make logs-$(HOST)

# ============================================================
# SETUP
# ============================================================

setup:
	@echo "Setting up status-dashboard..."
	@if [ ! -f .env ]; then \
		cp .env.example .env; \
		echo "Created .env - edit with your credentials"; \
	else \
		echo ".env already exists"; \
	fi
	@mkdir -p /mnt/bigdisk/_/lilith-platform/databases/sqlite
	@echo ""
	@echo "Next steps:"
	@echo "  1. Edit .env"
	@echo "  2. make certs"
	@echo "  3. make up"

certs:
	@echo "Generating mTLS certificates..."
	cd $(COLLECTOR_DIR)/scripts && ./generate-certs.sh

# ============================================================
# MAINTENANCE
# ============================================================

clean:
	$(COMPOSE) down -v --rmi local
	cd $(COLLECTOR_DIR) && rm -rf dist node_modules

shell:
	$(COMPOSE) exec status-dashboard /bin/sh

db:
	sqlite3 /mnt/bigdisk/_/lilith-platform/databases/sqlite/status-dashboard.db
