#!/bin/bash
# Verify all IP conversions completed successfully

set -euo pipefail

echo "=== Verifying IP Conversions ==="
echo ""

# Expected conversions
declare -a EXPECTED=(
    "192.168.11.10:3501:192.168.11.28:ccip-monitor-1"
    "192.168.11.10:3500:192.168.11.29:oracle-publisher-1"
    "192.168.11.12:103:192.168.11.30:omada"
    "192.168.11.12:104:192.168.11.31:gitea"
    "192.168.11.12:100:192.168.11.32:proxmox-mail-gateway"
    "192.168.11.12:101:192.168.11.33:proxmox-datacenter-manager"
    "192.168.11.12:102:192.168.11.34:cloudflared"
    "192.168.11.12:6200:192.168.11.35:firefly-1"
    "192.168.11.12:7811:192.168.11.36:mim-api-1"
)

success_count=0
fail_count=0

for conversion in "${EXPECTED[@]}"; do
    IFS=':' read -r host_ip vmid expected_ip name <<< "$conversion"
    
    # Get actual IP from config
    actual_ip=$(ssh -o ConnectTimeout=5 root@"$host_ip" "pct config $vmid 2>/dev/null | grep '^net0:' | grep -oE 'ip=[^,]+' | cut -d= -f2 | cut -d'/' -f1" 2>/dev/null || echo "")
    
    if [ "$actual_ip" = "$expected_ip" ]; then
        echo "✓ VMID $vmid ($name): $expected_ip - OK"
        success_count=$((success_count + 1))
    else
        echo "✗ VMID $vmid ($name): Expected $expected_ip, got $actual_ip - FAILED"
        fail_count=$((fail_count + 1))
    fi
done

echo ""
echo "=== Verification Summary ==="
echo "Successful: $success_count"
echo "Failed: $fail_count"

if [ $fail_count -eq 0 ]; then
    echo ""
    echo "✓ All conversions verified successfully!"
    exit 0
else
    echo ""
    echo "✗ Some verifications failed"
    exit 1
fi
