#!/bin/bash
# Review VM Deployment Details
# Analyzes each VM configuration and deployment steps

set -e

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

echo "=========================================="
echo "VM Deployment Review"
echo "=========================================="
echo ""

# Count VMs
TOTAL_VMS=$(find examples/production -name "*.yaml" -type f | wc -l)
echo -e "${BLUE}Total VMs to Deploy:${NC} $TOTAL_VMS"
echo ""

# Categorize VMs
echo -e "${BLUE}VM Categories:${NC}"
echo "  Core Infrastructure: $(ls -1 examples/production/*.yaml 2>/dev/null | wc -l)"
echo "  Phoenix Infrastructure: $(ls -1 examples/production/phoenix/*.yaml 2>/dev/null | wc -l)"
echo "  Blockchain Infrastructure: $(ls -1 examples/production/smom-dbis-138/*.yaml 2>/dev/null | wc -l)"
echo ""

# Review each VM
echo "=========================================="
echo "Detailed VM Configuration Review"
echo "=========================================="
echo ""

VM_COUNT=0
for file in $(find examples/production -name "*.yaml" -type f | sort); do
    VM_COUNT=$((VM_COUNT + 1))
    VM_NAME=$(basename "$file" .yaml)
    CATEGORY=$(dirname "$file" | sed 's|examples/production/||' | sed 's|^\.$|core|')
    
    echo -e "${BLUE}[$VM_COUNT/$TOTAL_VMS] $VM_NAME${NC}"
    echo "  Category: $CATEGORY"
    echo "  File: $file"
    
    # Extract configuration
    if kubectl apply --dry-run=client -f "$file" -o json 2>/dev/null > /tmp/vm_config.json; then
        NODE=$(jq -r '.spec.forProvider.node // "N/A"' /tmp/vm_config.json)
        SITE=$(jq -r '.spec.forProvider.site // "N/A"' /tmp/vm_config.json)
        CPU=$(jq -r '.spec.forProvider.cpu // "N/A"' /tmp/vm_config.json)
        MEMORY=$(jq -r '.spec.forProvider.memory // "N/A"' /tmp/vm_config.json)
        DISK=$(jq -r '.spec.forProvider.disk // "N/A"' /tmp/vm_config.json)
        STORAGE=$(jq -r '.spec.forProvider.storage // "N/A"' /tmp/vm_config.json)
        NETWORK=$(jq -r '.spec.forProvider.network // "N/A"' /tmp/vm_config.json)
        IMAGE=$(jq -r '.spec.forProvider.image // "N/A"' /tmp/vm_config.json)
        USERDATA=$(jq -r '.spec.forProvider.userData // ""' /tmp/vm_config.json | wc -c)
        
        echo "  Configuration:"
        echo "    Node: $NODE"
        echo "    Site: $SITE"
        echo "    CPU: $CPU cores"
        echo "    Memory: $MEMORY"
        echo "    Disk: $DISK"
        echo "    Storage: $STORAGE"
        echo "    Network: $NETWORK"
        echo "    Image: $IMAGE"
        if [ "$USERDATA" -gt 10 ]; then
            echo "    Cloud-init: ✅ Configured"
        else
            echo "    Cloud-init: ⚠️  Not configured"
        fi
        
        # Check current status
        if kubectl get proxmoxvm "$VM_NAME" -o json 2>/dev/null > /tmp/vm_status.json; then
            VMID=$(jq -r '.status.vmId // "pending"' /tmp/vm_status.json)
            STATE=$(jq -r '.status.state // "creating"' /tmp/vm_status.json)
            echo "  Status:"
            echo "    VMID: $VMID"
            echo "    State: $STATE"
        else
            echo "  Status: Not yet deployed"
        fi
        
        echo ""
    else
        echo -e "${RED}  Error: Cannot read configuration${NC}"
        echo ""
    fi
done

# Summary
echo "=========================================="
echo "Deployment Summary"
echo "=========================================="
echo ""
echo "Total VMs: $TOTAL_VMS"
echo ""

# Count by node
echo "VMs by Node:"
find examples/production -name "*.yaml" -type f -exec sh -c 'kubectl apply --dry-run=client -f "$1" -o json 2>/dev/null | jq -r ".spec.forProvider.node // \"unknown\""' _ {} \; | sort | uniq -c

echo ""
echo "VMs by Site:"
find examples/production -name "*.yaml" -type f -exec sh -c 'kubectl apply --dry-run=client -f "$1" -o json 2>/dev/null | jq -r ".spec.forProvider.site // \"unknown\""' _ {} \; | sort | uniq -c

echo ""
echo "VMs by Storage:"
find examples/production -name "*.yaml" -type f -exec sh -c 'kubectl apply --dry-run=client -f "$1" -o json 2>/dev/null | jq -r ".spec.forProvider.storage // \"unknown\""' _ {} \; | sort | uniq -c

rm -f /tmp/vm_config.json /tmp/vm_status.json

