# Docker-based Lux Universe Deployment
# 5 nodes validate everything: LUX mainnet + all app chains (Hanzo, ZOO, SPC)

.PHONY: all up down clean logs status test help

# Default
all: up

help:
	@echo "Lux Docker Universe Manager"
	@echo "==========================="
	@echo "Commands:"
	@echo "  make up      - Start all services (nodes, explorers, IPFS)"
	@echo "  make down    - Stop all services"
	@echo "  make clean   - Stop and remove volumes"
	@echo "  make logs    - Follow all logs"
	@echo "  make status  - Check service status"
	@echo "  make test    - Test all endpoints"
	@echo "  make build   - Rebuild Docker images"

# Build Docker images
build:
	@echo "Building Docker images..."
	docker compose build --parallel

# Start all services
up:
	@echo "Starting Lux Universe (5 nodes + all services)..."
	docker compose up -d postgres
	@sleep 5
	docker compose up -d ipfs
	@sleep 3
	docker compose up -d lux-node-1
	@sleep 5
	docker compose up -d lux-node-2 lux-node-3 lux-node-4 lux-node-5
	@sleep 10
	docker compose up -d lux-explorer hanzo-explorer zoo-explorer spc-explorer
	@echo ""
	@echo "✅ Lux Universe Started!"
	@echo ""
	@echo "Services:"
	@echo "  Nodes: http://localhost:9650,9660,9670,9680,9690"
	@echo "  Explorers:"
	@echo "    LUX: http://localhost:4000"
	@echo "    Hanzo: http://localhost:4003"
	@echo "    Zoo: http://localhost:4001"
	@echo "    SPC: http://localhost:4002"
	@echo "  IPFS: http://localhost:5001 (API), http://localhost:8080 (Gateway)"

# Stop all services
down:
	@echo "Stopping all services..."
	docker compose down

# Clean everything
clean:
	@echo "Cleaning everything..."
	docker compose down -v
	docker network prune -f

# View logs
logs:
	docker compose logs -f

# Check status
status:
	@echo "Service Status:"
	@echo "=============="
	@docker compose ps
	@echo ""
	@echo "Network Health:"
	@for i in 9650 9660 9670 9680 9690; do \
		echo -n "Node $$i: "; \
		curl -s http://localhost:$$i/v1/health | jq -r .healthy || echo "not ready"; \
	done

# Test all endpoints
test:
	@echo "Testing Endpoints..."
	@echo "==================="
	@echo ""
	@echo "Testing Nodes:"
	@for port in 9650 9660 9670 9680 9690; do \
		echo -n "  Port $$port: "; \
		if curl -s -X POST --data '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' \
			-H 'content-type:application/json' http://localhost:$$port/v1/chain/C/rpc | grep -q result; then \
			echo "✓ OK"; \
		else \
			echo "✗ Failed"; \
		fi; \
	done
	@echo ""
	@echo "Testing Explorers:"
	@for port in 4000 4001 4002 4003; do \
		echo -n "  Port $$port: "; \
		if curl -s http://localhost:$$port/api/v1/health | grep -q ok; then \
			echo "✓ OK"; \
		else \
			echo "✗ Failed"; \
		fi; \
	done
	@echo ""
	@echo "Testing IPFS:"
	@echo -n "  API: "
	@if curl -s http://localhost:5001/api/v0/version | grep -q Version; then \
		echo "✓ OK"; \
	else \
		echo "✗ Failed"; \
	fi

# Initialize databases
init-db:
	@echo "Initializing databases..."
	docker exec lux-postgres psql -U postgres -c "CREATE DATABASE explorer_luxnet IF NOT EXISTS;"
	docker exec lux-postgres psql -U postgres -c "CREATE DATABASE explorer_hanzo IF NOT EXISTS;"
	docker exec lux-postgres psql -U postgres -c "CREATE DATABASE explorer_zoo IF NOT EXISTS;"
	docker exec lux-postgres psql -U postgres -c "CREATE DATABASE explorer_spc IF NOT EXISTS;"

# Deploy app chains
deploy-chains:
	@echo "Deploying app chains on Lux mainnet..."
	@echo "Creating Hanzo chain..."
	@curl -X POST --data '{ \
		"jsonrpc": "2.0", \
		"method": "platform.createChain", \
		"params": { \
			"controlKeys": ["P-lux1q2z72rl2yqrfhkw8xfgn9qvxh7xr4n3zpjqkqw"], \
			"threshold": 1 \
		}, \
		"id": 1 \
	}' -H 'content-type:application/json' http://localhost:9650/v1/P
	@echo ""
	@echo "App chains deployed!"

.SILENT: help status test