#!/bin/bash
# Update SMOM-DBIS-138 VMs to match specifications

set -euo pipefail

echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  UPDATE SMOM-DBIS-138 VM SPECIFICATIONS"
echo "═══════════════════════════════════════════════════════════════════════════════"
echo ""

# Update Validators: 2 CPU → 6 CPU each (24 total)
echo "Step 1: Updating Validators (CPU: 2 → 6)..."
for i in 01 02 03 04; do
    kubectl patch proxmoxvm -n default smom-validator-$i --type='json' \
        -p='[{"op": "replace", "path": "/spec/forProvider/cpu", "value": 6}]' 2>/dev/null && \
        echo "  ✅ smom-validator-$i: CPU updated to 6" || \
        echo "  ❌ smom-validator-$i: Failed"
done
echo ""

# Update Sentries: 2 CPU → 4, 4 GiB RAM → 8, 20 GiB disk → 15
echo "Step 2: Updating Sentries (CPU: 2→4, RAM: 4→8, Disk: 20→15)..."
for i in 01 02 03 04; do
    kubectl patch proxmoxvm -n default smom-sentry-$i --type='json' \
        -p='[
            {"op": "replace", "path": "/spec/forProvider/cpu", "value": 4},
            {"op": "replace", "path": "/spec/forProvider/memory", "value": "8Gi"},
            {"op": "replace", "path": "/spec/forProvider/disk", "value": "15Gi"}
        ]' 2>/dev/null && \
        echo "  ✅ smom-sentry-$i: CPU=4, RAM=8Gi, Disk=15Gi" || \
        echo "  ❌ smom-sentry-$i: Failed"
done
echo ""

# Update RPC Nodes: 2 CPU → 4, 4 GiB RAM → 8, 20 GiB disk → 10
echo "Step 3: Updating RPC Nodes (CPU: 2→4, RAM: 4→8, Disk: 20→10)..."
for i in 01 02 03 04; do
    kubectl patch proxmoxvm -n default rpc-node-$i --type='json' \
        -p='[
            {"op": "replace", "path": "/spec/forProvider/cpu", "value": 4},
            {"op": "replace", "path": "/spec/forProvider/memory", "value": "8Gi"},
            {"op": "replace", "path": "/spec/forProvider/disk", "value": "10Gi"}
        ]' 2>/dev/null && \
        echo "  ✅ rpc-node-$i: CPU=4, RAM=8Gi, Disk=10Gi" || \
        echo "  ❌ rpc-node-$i: Failed"
done
echo ""

# Update Services: 2 CPU → 4, 4 GiB RAM → 8, 20 GiB disk → 35
echo "Step 4: Updating Services (CPU: 2→4, RAM: 4→8, Disk: 20→35)..."
kubectl patch proxmoxvm -n default smom-services --type='json' \
    -p='[
        {"op": "replace", "path": "/spec/forProvider/cpu", "value": 4},
        {"op": "replace", "path": "/spec/forProvider/memory", "value": "8Gi"},
        {"op": "replace", "path": "/spec/forProvider/disk", "value": "35Gi"}
    ]' 2>/dev/null && \
    echo "  ✅ smom-services: CPU=4, RAM=8Gi, Disk=35Gi" || \
    echo "  ❌ smom-services: Failed"
echo ""

# Update Blockscout: 2 CPU → 4, 4 GiB RAM → 8, 20 GiB disk → 12
echo "Step 5: Updating Blockscout (CPU: 2→4, RAM: 4→8, Disk: 20→12)..."
kubectl patch proxmoxvm -n default smom-blockscout --type='json' \
    -p='[
        {"op": "replace", "path": "/spec/forProvider/cpu", "value": 4},
        {"op": "replace", "path": "/spec/forProvider/memory", "value": "8Gi"},
        {"op": "replace", "path": "/spec/forProvider/disk", "value": "12Gi"}
    ]' 2>/dev/null && \
    echo "  ✅ smom-blockscout: CPU=4, RAM=8Gi, Disk=12Gi" || \
    echo "  ❌ smom-blockscout: Failed"
echo ""

# Update Monitoring: 2 CPU → 4, 4 GiB RAM → 8, 20 GiB disk → 9
echo "Step 6: Updating Monitoring (CPU: 2→4, RAM: 4→8, Disk: 20→9)..."
kubectl patch proxmoxvm -n default monitoring --type='json' \
    -p='[
        {"op": "replace", "path": "/spec/forProvider/cpu", "value": 4},
        {"op": "replace", "path": "/spec/forProvider/memory", "value": "8Gi"},
        {"op": "replace", "path": "/spec/forProvider/disk", "value": "9Gi"}
    ]' 2>/dev/null && \
    echo "  ✅ monitoring: CPU=4, RAM=8Gi, Disk=9Gi" || \
    echo "  ❌ monitoring: Failed"
echo ""

# Update Management: 20 GiB disk → 2
echo "Step 7: Updating Management (Disk: 20→2)..."
kubectl patch proxmoxvm -n default management --type='json' \
    -p='[{"op": "replace", "path": "/spec/forProvider/disk", "value": "2Gi"}]' 2>/dev/null && \
    echo "  ✅ management: Disk=2Gi" || \
    echo "  ❌ management: Failed"
echo ""

# Update all images to use template 9000
echo "Step 8: Updating all VMs to use template 9000..."
for vm in smom-validator-01 smom-validator-02 smom-validator-03 smom-validator-04 \
          smom-sentry-01 smom-sentry-02 smom-sentry-03 smom-sentry-04 \
          rpc-node-01 rpc-node-02 rpc-node-03 rpc-node-04 \
          smom-services smom-blockscout monitoring; do
    kubectl patch proxmoxvm -n default $vm --type='json' \
        -p='[{"op": "replace", "path": "/spec/forProvider/image", "value": "9000"}]' 2>/dev/null && \
        echo "  ✅ $vm: image=9000" || \
        echo "  ❌ $vm: Failed"
done
echo ""

echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  VERIFICATION"
echo "═══════════════════════════════════════════════════════════════════════════════"
echo ""
echo "Calculating totals..."
VALIDATORS_CPU=$(kubectl get proxmoxvms -n default -o json 2>/dev/null | jq -r '[.items[] | select(.metadata.name | startswith("smom-validator")) | .spec.forProvider.cpu] | add // 0')
SENTRIES_CPU=$(kubectl get proxmoxvms -n default -o json 2>/dev/null | jq -r '[.items[] | select(.metadata.name | startswith("smom-sentry")) | .spec.forProvider.cpu] | add // 0')
RPC_CPU=$(kubectl get proxmoxvms -n default -o json 2>/dev/null | jq -r '[.items[] | select(.metadata.name | startswith("rpc-node")) | .spec.forProvider.cpu] | add // 0')
SERVICES_CPU=$(kubectl get proxmoxvm -n default smom-services -o jsonpath='{.spec.forProvider.cpu}' 2>/dev/null || echo "0")
BLOCKSCOUT_CPU=$(kubectl get proxmoxvm -n default smom-blockscout -o jsonpath='{.spec.forProvider.cpu}' 2>/dev/null || echo "0")
MONITORING_CPU=$(kubectl get proxmoxvm -n default monitoring -o jsonpath='{.spec.forProvider.cpu}' 2>/dev/null || echo "0")
MANAGEMENT_CPU=$(kubectl get proxmoxvm -n default management -o jsonpath='{.spec.forProvider.cpu}' 2>/dev/null || echo "0")

TOTAL_CPU=$((VALIDATORS_CPU + SENTRIES_CPU + RPC_CPU + SERVICES_CPU + BLOCKSCOUT_CPU + MONITORING_CPU + MANAGEMENT_CPU))

echo "Total CPU: $TOTAL_CPU (Expected: 68)"
echo ""
echo "✅ Update complete! All VMs configured to match specifications."
echo ""
echo "📝 Next: Monitor deployment with: kubectl get proxmoxvms -A -w"

