#!/bin/bash
# VM Deployment Monitoring Script
# Continuously monitors VM deployment progress

echo "=========================================="
echo "📊 VM DEPLOYMENT MONITORING"
echo "=========================================="
echo ""
echo "Press Ctrl+C to stop"
echo ""

while true; do
    clear
    echo "=========================================="
    echo "📊 VM DEPLOYMENT STATUS - $(date '+%Y-%m-%d %H:%M:%S')"
    echo "=========================================="
    echo ""
    
    # Authentication Status
    echo "1. AUTHENTICATION STATUS"
    echo "----------------------------------------"
    AUTH_ERR=$(kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --since=2m 2>&1 | grep -i "invalid PVE ticket" | wc -l)
    echo "   Errors (last 2m): $AUTH_ERR"
    if [ $AUTH_ERR -eq 0 ]; then
        echo "   Status: ✅ Working"
    else
        echo "   Status: ⚠️  Issues detected"
    fi
    echo ""
    
    # Provider Status
    echo "2. PROVIDER STATUS"
    echo "----------------------------------------"
    PROVIDER_STATUS=$(kubectl get pods -n crossplane-system -l app=crossplane-provider-proxmox -o jsonpath='{.items[0].status.phase}' 2>/dev/null)
    PROVIDER_READY=$(kubectl get pods -n crossplane-system -l app=crossplane-provider-proxmox -o jsonpath='{.items[0].status.containerStatuses[0].ready}' 2>/dev/null)
    echo "   Status: $PROVIDER_STATUS"
    echo "   Ready: $PROVIDER_READY"
    echo ""
    
    # VM Creation Status
    echo "3. VM CREATION STATUS"
    echo "----------------------------------------"
    TOTAL=$(kubectl get proxmoxvm -A --no-headers 2>&1 | wc -l)
    WITH_VMID=$(kubectl get proxmoxvm -A -o jsonpath='{range .items[*]}{.status.vmId}{"\n"}{end}' 2>&1 | grep -v '^$' | wc -l)
    PENDING=$((TOTAL - WITH_VMID))
    if [ $TOTAL -gt 0 ]; then
        PERCENT=$((WITH_VMID * 100 / TOTAL))
    else
        PERCENT=0
    fi
    echo "   Total VMs: $TOTAL"
    echo "   Created (with VMID): $WITH_VMID"
    echo "   Pending: $PENDING"
    echo "   Progress: $PERCENT%"
    echo ""
    
    # Recent VMs Created
    if [ $WITH_VMID -gt 0 ]; then
        echo "   Recently Created VMs:"
        kubectl get proxmoxvm -A -o custom-columns=NAME:.metadata.name,NODE:.spec.forProvider.node,VMID:.status.vmId 2>&1 | grep -v "VMID.*<none>" | head -10
        echo ""
    fi
    
    # Node Health Checks
    echo "4. NODE HEALTH CHECKS"
    echo "----------------------------------------"
    HEALTH_CHECKS=$(kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --since=2m 2>&1 | grep -E "node.*healthy|node.*online" -i | wc -l)
    HEALTH_FAILS=$(kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --since=2m 2>&1 | grep -i "node health check failed" | wc -l)
    echo "   Successful: $HEALTH_CHECKS"
    echo "   Failed: $HEALTH_FAILS"
    echo ""
    
    # Recent Activity
    echo "5. RECENT ACTIVITY"
    echo "----------------------------------------"
    kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --since=1m 2>&1 | grep -E "Creating VM|VM.*created|VMID|Reconciling" -i | tail -5 || echo "   No recent activity"
    echo ""
    
    # Resource Allocation
    echo "6. RESOURCE ALLOCATION"
    echo "----------------------------------------"
    ML_CPU=$(kubectl get proxmoxvm -A -o jsonpath='{range .items[?(@.spec.forProvider.node=="ml110-01")]}{.spec.forProvider.cpu}{"\n"}{end}' 2>&1 | awk '{sum+=$1} END {print sum}')
    R_CPU=$(kubectl get proxmoxvm -A -o jsonpath='{range .items[?(@.spec.forProvider.node=="r630-01")]}{.spec.forProvider.cpu}{"\n"}{end}' 2>&1 | awk '{sum+=$1} END {print sum}')
    echo "   ML110-01: $ML_CPU CPU (5-6 available)"
    echo "   R630-01: $R_CPU CPU (52 available)"
    echo ""
    
    echo "=========================================="
    echo "Refreshing in 10 seconds... (Ctrl+C to stop)"
    echo "=========================================="
    
    sleep 10
done
