#!/bin/bash
# Complete verification and start for VM 100
# Run on Proxmox node: root@ml110-01

set -e

VMID=100
VM_NAME="basic-vm-001"

echo "=========================================="
echo "VM 100 Complete Verification and Start"
echo "=========================================="
echo ""

# Step 1: Check VM status
echo "Step 1: VM Status"
echo "--------------------------------------"
qm status $VMID
echo ""

# Step 2: Verify all critical configurations
echo "Step 2: Critical Configuration Checks"
echo "--------------------------------------"

# Boot order
BOOT_ORDER=$(qm config $VMID | grep '^boot:' || echo "")
if [ -z "$BOOT_ORDER" ]; then
    echo "⚠️  Boot order not set - fixing..."
    qm set $VMID --boot order=scsi0
    echo "✅ Boot order set"
else
    echo "✅ Boot order: $BOOT_ORDER"
fi

# Disk configuration
SCSI0=$(qm config $VMID | grep '^scsi0:' || echo "")
if [ -z "$SCSI0" ]; then
    echo "❌ ERROR: scsi0 disk not configured!"
    exit 1
else
    echo "✅ Disk: $SCSI0"
    # Check if disk exists
    DISK_NAME=$(echo "$SCSI0" | sed -n 's/.*local-lvm:\(vm-[0-9]*-disk-[0-9]*\).*/\1/p')
    if [ -n "$DISK_NAME" ]; then
        if lvs | grep -q "$DISK_NAME"; then
            DISK_SIZE=$(lvs | grep "$DISK_NAME" | awk '{print $4}')
            echo "   ✅ Disk exists: $DISK_NAME ($DISK_SIZE)"
        else
            echo "   ❌ ERROR: Disk $DISK_NAME not found!"
            exit 1
        fi
    fi
fi

# Cloud-init
IDE2=$(qm config $VMID | grep '^ide2:' || echo "")
CIUSER=$(qm config $VMID | grep '^ciuser:' || echo "")
if [ -z "$IDE2" ]; then
    echo "⚠️  Cloud-init drive not configured - fixing..."
    qm set $VMID --ide2 local-lvm:cloudinit
    echo "✅ Cloud-init drive configured"
else
    echo "✅ Cloud-init drive: $IDE2"
fi

if [ -z "$CIUSER" ]; then
    echo "⚠️  Cloud-init user not configured - fixing..."
    qm set $VMID --ciuser admin
    echo "✅ Cloud-init user configured"
else
    echo "✅ Cloud-init user: $CIUSER"
fi

IPCONFIG=$(qm config $VMID | grep '^ipconfig0:' || echo "")
if [ -z "$IPCONFIG" ]; then
    echo "⚠️  IP config not set - fixing..."
    qm set $VMID --ipconfig0 ip=dhcp
    echo "✅ IP config set"
else
    echo "✅ IP config: $IPCONFIG"
fi

# Network
NET0=$(qm config $VMID | grep '^net0:' || echo "")
if [ -z "$NET0" ]; then
    echo "⚠️  Network not configured - fixing..."
    qm set $VMID --net0 virtio,bridge=vmbr0
    echo "✅ Network configured"
else
    echo "✅ Network: $NET0"
fi

# Guest agent (already fixed, but verify)
AGENT=$(qm config $VMID | grep '^agent:' || echo "")
if [ -z "$AGENT" ]; then
    echo "⚠️  Guest agent not enabled - fixing..."
    qm set $VMID --agent 1
    echo "✅ Guest agent enabled"
else
    echo "✅ Guest agent: $AGENT"
fi

echo ""

# Step 3: Final configuration summary
echo "Step 3: Final Configuration Summary"
echo "--------------------------------------"
qm config $VMID | grep -E '^agent:|^boot:|^scsi0:|^ide2:|^net0:|^ciuser:' | while read line; do
    echo "  $line"
done
echo ""

# Step 4: Start VM
echo "Step 4: Starting VM"
echo "--------------------------------------"
CURRENT_STATUS=$(qm status $VMID | awk '{print $2}')
if [ "$CURRENT_STATUS" = "running" ]; then
    echo "✅ VM is already running"
else
    echo "Current status: $CURRENT_STATUS"
    echo "Starting VM..."
    qm start $VMID
    echo ""
    echo "Waiting 5 seconds for initialization..."
    sleep 5
    echo ""
    echo "VM status after start:"
    qm status $VMID
fi
echo ""

# Step 5: Monitoring instructions
echo "=========================================="
echo "VM Started - Monitoring Instructions"
echo "=========================================="
echo ""
echo "Monitor from Proxmox node:"
echo "  watch -n 2 'qm status $VMID'"
echo ""
echo "Monitor from Kubernetes:"
echo "  kubectl get proxmoxvm $VM_NAME -w"
echo ""
echo "Check provider logs:"
echo "  kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --tail=50 -f"
echo ""
echo "Once VM has IP, verify services:"
echo "  IP=\$(kubectl get proxmoxvm $VM_NAME -o jsonpath='{.status.networkInterfaces[0].ipAddress}')"
echo "  ssh admin@\$IP 'systemctl status qemu-guest-agent chrony unattended-upgrades'"
echo ""

