#!/bin/bash
# Helper script to find available VMIDs for bridge frontend deployment

PROXMOX_HOST="${1:-192.168.11.12}"

echo "🔍 Finding available VMIDs for bridge frontend deployment..."
echo ""

if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pveversion >/dev/null 2>&1" 2>/dev/null; then
    echo "❌ Cannot connect to Proxmox host: $PROXMOX_HOST"
    echo "   Please check SSH connectivity and host address"
    exit 1
fi

echo "📋 Available VMIDs on $PROXMOX_HOST:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct list | grep -E '^[0-9]+'" 2>/dev/null | while read -r line; do
    VMID=$(echo "$line" | awk '{print $1}')
    STATUS=$(echo "$line" | awk '{print $2}')
    NAME=$(echo "$line" | awk '{print $2}' | tail -c +4 || echo "unknown")
    
    # Get VM config to check IP
    CONFIG=$(ssh -o ConnectTimeout=5 root@"$PROXMOX_HOST" "pct config $VMID 2>/dev/null" | grep -oP 'ip=\K[^/]+' | head -1 || echo "")
    
    if [ "$VMID" != "7810" ]; then
        echo "  VMID: $VMID | Status: $STATUS | IP: ${CONFIG:-N/A}"
    else
        echo "  VMID: $VMID | Status: $STATUS | IP: ${CONFIG:-N/A} ⚠️  (Reserved for mim4u.org)"
    fi
done

echo ""
echo "💡 Recommendation:"
echo "  - Use an existing VMID with 'running' status, OR"
echo "  - Create a new VMID for bridge frontend deployment"
echo ""
echo "Example deployment:"
echo "  ./deploy.sh $PROXMOX_HOST [VMID]"
echo ""
