#!/bin/bash
# Deploy All Production VMs
# Deploys all VMs in the correct order according to the deployment plan

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

echo "=========================================="
echo "VM Deployment Script"
echo "=========================================="
echo ""

# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
    echo -e "${RED}Error: kubectl not found${NC}"
    exit 1
fi

# Check if we can connect to cluster
if ! kubectl cluster-info &> /dev/null; then
    echo -e "${RED}Error: Cannot connect to Kubernetes cluster${NC}"
    exit 1
fi

# Function to deploy and wait
deploy_vm() {
    local file=$1
    local name=$(basename "$file" .yaml)
    
    echo -e "${BLUE}Deploying $name...${NC}"
    if kubectl apply -f "$file" &>/dev/null; then
        echo -e "${GREEN}✓${NC} $name deployed"
        return 0
    else
        echo -e "${RED}✗${NC} $name deployment failed"
        return 1
    fi
}

# Phase 1: Core Infrastructure
echo "=========================================="
echo "Phase 1: Core Infrastructure"
echo "=========================================="
echo ""

deploy_vm "$PROJECT_ROOT/examples/production/nginx-proxy-vm.yaml"
deploy_vm "$PROJECT_ROOT/examples/production/phoenix/dns-primary.yaml"
deploy_vm "$PROJECT_ROOT/examples/production/cloudflare-tunnel-vm.yaml"

echo ""
sleep 2

# Phase 2: Phoenix Infrastructure
echo "=========================================="
echo "Phase 2: Phoenix Infrastructure"
echo "=========================================="
echo ""

PHOENIX_VMS=(
    "examples/production/phoenix/git-server.yaml"
    "examples/production/phoenix/email-server.yaml"
    "examples/production/phoenix/devops-runner.yaml"
    "examples/production/phoenix/codespaces-ide.yaml"
    "examples/production/phoenix/as4-gateway.yaml"
    "examples/production/phoenix/business-integration-gateway.yaml"
    "examples/production/phoenix/financial-messaging-gateway.yaml"
)

for vm in "${PHOENIX_VMS[@]}"; do
    deploy_vm "$PROJECT_ROOT/$vm"
done

echo ""
sleep 2

# Phase 3: Blockchain Infrastructure
echo "=========================================="
echo "Phase 3: Blockchain Infrastructure"
echo "=========================================="
echo ""

# Validators
echo -e "${BLUE}Deploying Validators...${NC}"
for i in {1..4}; do
    deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/validator-0$i.yaml"
done

echo ""

# Sentries
echo -e "${BLUE}Deploying Sentries...${NC}"
for i in {1..4}; do
    deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/sentry-0$i.yaml"
done

echo ""

# RPC Nodes
echo -e "${BLUE}Deploying RPC Nodes...${NC}"
for i in {1..4}; do
    deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/rpc-node-0$i.yaml"
done

echo ""

# Services
echo -e "${BLUE}Deploying Services...${NC}"
deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/management.yaml"
deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/monitoring.yaml"
deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/services.yaml"
deploy_vm "$PROJECT_ROOT/examples/production/smom-dbis-138/blockscout.yaml"

echo ""
echo "=========================================="
echo "Deployment Complete"
echo "=========================================="
echo ""

# Show status
echo "VM Status:"
kubectl get proxmoxvm -A --sort-by=.metadata.name 2>&1 | head -35

echo ""
echo "To monitor deployment:"
echo "  kubectl get proxmoxvm -A -w"
echo ""
echo "To check provider logs:"
echo "  kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox -f"

