#!/bin/bash
# Update all VM template files to match current Kubernetes configurations

set -euo pipefail

TEMPLATE_DIR="examples/production"

echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  UPDATE ALL VM TEMPLATE FILES"
echo "═══════════════════════════════════════════════════════════════════════════════"
echo ""

# Function to create/update a VM template file
update_vm_template() {
    local vm_name=$1
    local namespace=$2
    local node=$3
    local cpu=$4
    local memory=$5
    local disk=$6
    local storage=$7
    local image=$8
    local network=$9
    local site=${10}
    local file_path=$11
    
    # Ensure directory exists
    mkdir -p "$(dirname "$file_path")"
    
    # Create/update the template file
    cat > "$file_path" <<EOF
apiVersion: proxmox.sankofa.nexus/v1alpha1
kind: ProxmoxVM
metadata:
  name: $vm_name
  namespace: $namespace
spec:
  forProvider:
    node: $node
    name: $vm_name
    cpu: $cpu
    memory: $memory
    disk: $disk
    storage: $storage
    network: $network
    image: $image
    site: $site
EOF
    
    echo "  ✅ Updated: $file_path"
}

# Get all VM configurations from Kubernetes
echo "Step 1: Fetching VM configurations from Kubernetes..."
VM_CONFIGS=$(kubectl get proxmoxvms -A -o json 2>/dev/null | jq -r '.items[] | 
    select(.spec.forProvider.node == "r630-01" or .spec.forProvider.node == "ml110-01") | 
    "\(.metadata.namespace)|\(.metadata.name)|\(.spec.forProvider.node)|\(.spec.forProvider.cpu)|\(.spec.forProvider.memory)|\(.spec.forProvider.disk)|\(.spec.forProvider.storage)|\(.spec.forProvider.image)|\(.spec.forProvider.network)|\(.spec.forProvider.site)"' | sort)

echo "Found $(echo "$VM_CONFIGS" | wc -l) VMs to update"
echo ""

# Update SMOM-DBIS-138 VMs
echo "Step 2: Updating SMOM-DBIS-138 VM templates..."
while IFS='|' read -r namespace name node cpu memory disk storage image network site; do
    if [[ "$name" =~ ^(smom-validator|smom-sentry|rpc-node|smom-services|smom-blockscout|monitoring|management)$ ]]; then
        # Map Kubernetes names to template file names
        case "$name" in
            smom-validator-*)
                file_name="validator-${name##smom-validator-}.yaml"
                ;;
            smom-sentry-*)
                file_name="sentry-${name##smom-sentry-}.yaml"
                ;;
            rpc-node-*)
                file_name="rpc-node-${name##rpc-node-}.yaml"
                ;;
            smom-services)
                file_name="services.yaml"
                ;;
            smom-blockscout)
                file_name="blockscout.yaml"
                ;;
            monitoring)
                file_name="monitoring.yaml"
                ;;
            management)
                file_name="management.yaml"
                ;;
            *)
                continue
                ;;
        esac
        
        file_path="$TEMPLATE_DIR/smom-dbis-138/$file_name"
        update_vm_template "$name" "$namespace" "$node" "$cpu" "$memory" "$disk" "$storage" "$image" "$network" "$site" "$file_path"
    fi
done <<< "$VM_CONFIGS"
echo ""

# Update Phoenix VMs
echo "Step 3: Updating Phoenix VM templates..."
while IFS='|' read -r namespace name node cpu memory disk storage image network site; do
    if [[ "$name" =~ ^phoenix- ]]; then
        # Map Kubernetes names to template file names
        file_name="${name##phoenix-}.yaml"
        file_path="$TEMPLATE_DIR/phoenix/$file_name"
        update_vm_template "$name" "$namespace" "$node" "$cpu" "$memory" "$disk" "$storage" "$image" "$network" "$site" "$file_path"
    fi
done <<< "$VM_CONFIGS"
echo ""

# Update other production VMs
echo "Step 4: Updating other production VM templates..."
while IFS='|' read -r namespace name node cpu memory disk storage image network site; do
    # Skip SMOM and Phoenix VMs (already handled)
    if [[ "$name" =~ ^(smom-|phoenix-|rpc-node|monitoring|management)$ ]]; then
        continue
    fi
    
    # Skip example/template VMs that aren't meant for production
    if [[ "$name" =~ ^(basic-vm-001|large-vm-001|medium-vm-001|vm-100)$ ]]; then
        continue
    fi
    
    # Map to template file names
    case "$name" in
        nginx-proxy-vm)
            file_name="nginx-proxy-vm.yaml"
            ;;
        cloudflare-tunnel-vm)
            file_name="cloudflare-tunnel-vm.yaml"
            ;;
        *)
            # Use name as-is for other VMs
            file_name="${name}.yaml"
            ;;
    esac
    
    file_path="$TEMPLATE_DIR/$file_name"
    update_vm_template "$name" "$namespace" "$node" "$cpu" "$memory" "$disk" "$storage" "$image" "$network" "$site" "$file_path"
done <<< "$VM_CONFIGS"
echo ""

echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  SUMMARY"
echo "═══════════════════════════════════════════════════════════════════════════════"
echo ""
echo "✅ All VM template files updated!"
echo ""
echo "📝 Updated templates:"
echo "  - SMOM-DBIS-138 VMs: $(find $TEMPLATE_DIR/smom-dbis-138 -name "*.yaml" | wc -l) files"
echo "  - Phoenix VMs: $(find $TEMPLATE_DIR/phoenix -name "*.yaml" | wc -l) files"
echo "  - Other production VMs: $(find $TEMPLATE_DIR -maxdepth 1 -name "*.yaml" | wc -l) files"
echo ""
echo "All templates now use:"
echo "  - Storage: local-lvm"
echo "  - Image: 9000 (template) or local:iso/ubuntu-22.04-cloud.img"
echo "  - Network: vmbr0"
echo "  - Current CPU, RAM, and Disk specifications"

