#!/usr/bin/env bash
# Deploy Chainlink CCIP Nodes on Proxmox VE LXC containers
# Implements batch deployment for CCIP nodes (41-43 containers)

set -euo pipefail

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

source "$PROJECT_ROOT/lib/common.sh" 2>/dev/null || {
    log_info() { echo "[INFO] $1"; }
    log_success() { echo "[✓] $1"; }
    log_error() { echo "[ERROR] $1"; exit 1; }
    log_warn() { echo "[WARN] $1"; }
}

# Load configuration
load_config "$PROJECT_ROOT/config/proxmox.conf" 2>/dev/null || true

# CCIP node counts from config
CCIP_OPS_COUNT="${CCIP_OPS_COUNT:-2}"
CCIP_MON_COUNT="${CCIP_MON_COUNT:-2}"
CCIP_COMMIT_COUNT="${CCIP_COMMIT_COUNT:-16}"
CCIP_EXEC_COUNT="${CCIP_EXEC_COUNT:-16}"
CCIP_RMN_COUNT="${CCIP_RMN_COUNT:-5}"

# VMID ranges
VMID_CCIP_OPS_START="${VMID_CCIP_OPS_START:-5400}"
VMID_CCIP_MON_START="${VMID_CCIP_MON_START:-5402}"
VMID_CCIP_COMMIT_START="${VMID_CCIP_COMMIT_START:-5410}"
VMID_CCIP_EXEC_START="${VMID_CCIP_EXEC_START:-5440}"
VMID_CCIP_RMN_START="${VMID_CCIP_RMN_START:-5470}"

# Parallel execution limits
BATCH_SIZE="${CCIP_BATCH_SIZE:-8}"
MAX_PARALLEL="${MAX_PARALLEL_CCIP:-8}"

log_info "========================================="
log_info "CCIP Nodes Deployment"
log_info "========================================="
log_info ""
log_info "Node Counts:"
log_info "  OPS: $CCIP_OPS_COUNT"
log_info "  MON: $CCIP_MON_COUNT"
log_info "  COMMIT: $CCIP_COMMIT_COUNT"
log_info "  EXEC: $CCIP_EXEC_COUNT"
log_info "  RMN: $CCIP_RMN_COUNT"
log_info ""
log_info "Batch Size: $BATCH_SIZE"
log_info "Max Parallel: $MAX_PARALLEL"
log_info ""

# Check if running on Proxmox host
if ! command_exists pct; then
    log_error "pct command not found. This script must be run on Proxmox host."
fi

if [[ $EUID -ne 0 ]]; then
    log_error "This script must be run as root"
fi

# Function to create CCIP node container
create_ccip_node() {
    local node_type="$1"  # ops, mon, commit, exec, rmn
    local vmid="$2"
    local hostname="$3"
    local memory="${4:-4096}"
    local cores="${5:-4}"
    local disk="${6:-100}"
    
    log_info "Creating CCIP $node_type node: $hostname (VMID: $vmid)"
    
    # Use DHCP for network configuration
    local network_config="bridge=${PROXMOX_BRIDGE:-vmbr0},name=eth0,ip=dhcp,type=veth"
    
    if pct list | grep -q "^\s*$vmid\s"; then
        log_warn "Container $vmid already exists, skipping creation"
        return 0
    fi
    
    # Create container
    pct create "$vmid" \
        "${CONTAINER_OS_TEMPLATE:-local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst}" \
        --storage "${PROXMOX_STORAGE:-local-lvm}" \
        --hostname "$hostname" \
        --memory "$memory" \
        --cores "$cores" \
        --rootfs "${PROXMOX_STORAGE:-local-lvm}:${disk}" \
        --net0 "$network_config" \
        --unprivileged "${CONTAINER_UNPRIVILEGED:-1}" \
        --swap "${CONTAINER_SWAP:-512}" \
        --onboot "${CONTAINER_ONBOOT:-1}" \
        --timezone "${CONTAINER_TIMEZONE:-America/Los_Angeles}" \
        --features nesting=1,keyctl=1
    
    log_success "Container $vmid created"
    
    # Wait for container to be ready
    sleep 2
    
    # Start container
    pct start "$vmid" || {
        log_error "Failed to start container $vmid"
        return 1
    }
    
    # Wait for container to be ready
    local max_wait=60
    local waited=0
    while ! pct exec "$vmid" -- true 2>/dev/null && [[ $waited -lt $max_wait ]]; do
        sleep 2
        waited=$((waited + 2))
    done
    
    if ! pct exec "$vmid" -- true 2>/dev/null; then
        log_error "Container $vmid not ready after ${max_wait}s"
        return 1
    fi
    
    log_success "CCIP $node_type node $hostname (VMID: $vmid) created and started"
    echo "$vmid:$hostname"
}

# Export function for parallel execution
export -f create_ccip_node

# Deploy CCIP-OPS nodes (2 nodes, parallel)
log_info "========================================="
log_info "Deploying CCIP-OPS Nodes ($CCIP_OPS_COUNT nodes)"
log_info "========================================="

ops_args=()
for i in $(seq 0 $((CCIP_OPS_COUNT - 1))); do
    vmid=$((VMID_CCIP_OPS_START + i))
    hostname="CCIP-OPS-$(printf "%02d" $((i + 1)))"
    ops_args+=("ops $vmid $hostname 4096 4 100")
done

if command_exists parallel_execute_with_progress 2>/dev/null; then
    source "$PROJECT_ROOT/lib/batch-parallel.sh"
    parallel_execute_with_progress "create_ccip_node" ops_args $CCIP_OPS_COUNT
else
    # Fallback to sequential
    for args in "${ops_args[@]}"; do
        create_ccip_node $args
    done
fi

log_success "CCIP-OPS nodes deployed"

# Deploy CCIP-MON nodes (2 nodes, parallel)
log_info ""
log_info "========================================="
log_info "Deploying CCIP-MON Nodes ($CCIP_MON_COUNT nodes)"
log_info "========================================="

mon_args=()
for i in $(seq 0 $((CCIP_MON_COUNT - 1))); do
    vmid=$((VMID_CCIP_MON_START + i))
    hostname="CCIP-MON-$(printf "%02d" $((i + 1)))"
    mon_args+=("mon $vmid $hostname 4096 4 100")
done

if command_exists parallel_execute_with_progress 2>/dev/null; then
    parallel_execute_with_progress "create_ccip_node" mon_args $CCIP_MON_COUNT
else
    for args in "${mon_args[@]}"; do
        create_ccip_node $args
    done
fi

log_success "CCIP-MON nodes deployed"

# Deploy CCIP-COMMIT nodes (16 nodes, batched)
log_info ""
log_info "========================================="
log_info "Deploying CCIP-COMMIT Nodes ($CCIP_COMMIT_COUNT nodes in batches of $BATCH_SIZE)"
log_info "========================================="

commit_args=()
for i in $(seq 0 $((CCIP_COMMIT_COUNT - 1))); do
    vmid=$((VMID_CCIP_COMMIT_START + i))
    hostname="CCIP-COMMIT-$(printf "%02d" $((i + 1)))"
    commit_args+=("commit $vmid $hostname 8192 4 150")
done

if command_exists batch_parallel_execute 2>/dev/null; then
    source "$PROJECT_ROOT/lib/batch-parallel.sh"
    batch_parallel_execute "create_ccip_node" commit_args $BATCH_SIZE $MAX_PARALLEL
else
    # Fallback to sequential batching
    for i in $(seq 0 $BATCH_SIZE $((CCIP_COMMIT_COUNT - 1))); do
        batch_end=$((i + BATCH_SIZE))
        [[ $batch_end -gt $CCIP_COMMIT_COUNT ]] && batch_end=$CCIP_COMMIT_COUNT
        log_info "Processing batch: nodes $((i + 1))-$batch_end"
        for j in $(seq $i $((batch_end - 1))); do
            create_ccip_node "${commit_args[$j]}"
        done
    done
fi

log_success "CCIP-COMMIT nodes deployed"

# Deploy CCIP-EXEC nodes (16 nodes, batched)
log_info ""
log_info "========================================="
log_info "Deploying CCIP-EXEC Nodes ($CCIP_EXEC_COUNT nodes in batches of $BATCH_SIZE)"
log_info "========================================="

exec_args=()
for i in $(seq 0 $((CCIP_EXEC_COUNT - 1))); do
    vmid=$((VMID_CCIP_EXEC_START + i))
    hostname="CCIP-EXEC-$(printf "%02d" $((i + 1)))"
    exec_args+=("exec $vmid $hostname 8192 4 150")
done

if command_exists batch_parallel_execute 2>/dev/null; then
    batch_parallel_execute "create_ccip_node" exec_args $BATCH_SIZE $MAX_PARALLEL
else
    # Fallback to sequential batching
    for i in $(seq 0 $BATCH_SIZE $((CCIP_EXEC_COUNT - 1))); do
        batch_end=$((i + BATCH_SIZE))
        [[ $batch_end -gt $CCIP_EXEC_COUNT ]] && batch_end=$CCIP_EXEC_COUNT
        log_info "Processing batch: nodes $((i + 1))-$batch_end"
        for j in $(seq $i $((batch_end - 1))); do
            create_ccip_node "${exec_args[$j]}"
        done
    done
fi

log_success "CCIP-EXEC nodes deployed"

# Deploy CCIP-RMN nodes (5-7 nodes, parallel)
log_info ""
log_info "========================================="
log_info "Deploying CCIP-RMN Nodes ($CCIP_RMN_COUNT nodes)"
log_info "========================================="

rmn_args=()
for i in $(seq 0 $((CCIP_RMN_COUNT - 1))); do
    vmid=$((VMID_CCIP_RMN_START + i))
    hostname="CCIP-RMN-$(printf "%02d" $((i + 1)))"
    rmn_args+=("rmn $vmid $hostname 8192 4 150")
done

if command_exists parallel_execute_with_progress 2>/dev/null; then
    parallel_execute_with_progress "create_ccip_node" rmn_args $CCIP_RMN_COUNT
else
    for args in "${rmn_args[@]}"; do
        create_ccip_node $args
    done
fi

log_success "CCIP-RMN nodes deployed"

log_info ""
log_success "========================================="
log_success "CCIP Nodes Deployment Complete"
log_success "========================================="
log_info ""
log_info "Total CCIP nodes deployed: $((CCIP_OPS_COUNT + CCIP_MON_COUNT + CCIP_COMMIT_COUNT + CCIP_EXEC_COUNT + CCIP_RMN_COUNT))"
log_info ""
log_info "Next steps:"
log_info "  1. Install Chainlink dependencies in containers"
log_info "  2. Copy CCIP configuration files"
log_info "  3. Configure Chainlink nodes"
log_info "  4. Initialize DONs (Commit, Execute, RMN)"

