#!/usr/bin/env bash
# Direct container creation script for ChainID 138
# Creates all 13 missing containers with correct specifications

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
TEMPLATE="local:vztmpl/debian-12-standard_12.12-1_amd64.tar.zst"
STORAGE="local-lvm"
NETWORK="vmbr0"
GATEWAY="192.168.11.1"

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

log() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }

# Check if container exists
exists() {
    local vmid=$1
    ssh -o StrictHostKeyChecking=no root@${PROXMOX_HOST} "pct list | grep -q '^$vmid ' && echo 'yes' || echo 'no'"
}

# Create container
create() {
    local vmid=$1
    local hostname=$2
    local ip=$3
    local mem=$4
    local cores=$5
    local disk=$6
    local desc="$7"
    
    if [[ "$(exists $vmid)" == "yes" ]]; then
        warn "Container $vmid already exists, skipping..."
        return 0
    fi
    
    log "Creating $vmid: $hostname ($ip) - ${mem}GB RAM, ${cores} cores, ${disk}GB disk..."
    
    ssh -o StrictHostKeyChecking=no root@${PROXMOX_HOST} <<EOF
pct create $vmid $TEMPLATE \
    --hostname $hostname \
    --memory $((mem * 1024)) \
    --cores $cores \
    --rootfs $STORAGE:${disk} \
    --net0 name=eth0,bridge=$NETWORK,ip=$ip/24,gw=$GATEWAY \
    --description "$desc" \
    --start 1 \
    --onboot 1 \
    --unprivileged 0 2>&1 | grep -v "WARNING: Ignoring duplicate" || true
EOF
    
    if [[ "$(exists $vmid)" == "yes" ]]; then
        success "Container $vmid created successfully"
        return 0
    else
        error "Failed to create container $vmid"
        return 1
    fi
}

log "Creating ChainID 138 containers on $PROXMOX_HOST..."
echo ""

# Besu Sentry
create 1504 "besu-sentry-5" "192.168.11.154" 4 2 100 "Besu Sentry Node - Ali's dedicated host"

# Besu RPC Nodes
create 2503 "besu-rpc-4" "192.168.11.253" 16 4 200 "Besu RPC Node - Ali (0x8a identity)"
create 2504 "besu-rpc-4" "192.168.11.254" 16 4 200 "Besu RPC Node - Ali (0x1 identity)"
create 2505 "besu-rpc-luis" "192.168.11.255" 16 4 200 "Besu RPC Node - Luis (0x8a identity)"
create 2506 "besu-rpc-luis" "192.168.11.256" 16 4 200 "Besu RPC Node - Luis (0x1 identity)"
create 2507 "besu-rpc-putu" "192.168.11.257" 16 4 200 "Besu RPC Node - Putu (0x8a identity)"
create 2508 "besu-rpc-putu" "192.168.11.258" 16 4 200 "Besu RPC Node - Putu (0x1 identity)"

# Hyperledger Services
create 6200 "firefly-1" "192.168.11.66" 4 2 50 "Hyperledger Firefly Core"
create 6201 "firefly-2" "192.168.11.67" 4 2 50 "Hyperledger Firefly Node - ChainID 138"
create 5200 "cacti-1" "192.168.11.64" 4 2 50 "Hyperledger Cacti"
create 6000 "fabric-1" "192.168.11.65" 8 4 100 "Hyperledger Fabric"
create 6400 "indy-1" "192.168.11.68" 8 4 100 "Hyperledger Indy"

# Explorer
create 5000 "blockscout-1" "192.168.11.69" 8 4 200 "Blockscout Explorer - ChainID 138"

echo ""
log "Container creation complete!"
log "Checking final status..."
ssh -o StrictHostKeyChecking=no root@${PROXMOX_HOST} "pct list | grep -E '(1504|2503|2504|2505|2506|2507|2508|6200|6201|5200|6000|6400|5000)' | sort -n"

