#!/bin/bash
# Check the actual IP address assigned to Blockscout container (VMID 5000)

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
VMID=5000

echo "Checking actual IP address for Blockscout (VMID $VMID)..."
echo ""

# Find which node has the container
CONTAINER_NODE=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "for node in ml110 pve pve2; do \
        if pvesh get /nodes/\$node/lxc/$VMID/status/current 2>/dev/null | grep -q status; then \
            echo \$node; break; \
        fi; \
    done" 2>/dev/null || echo "")

if [ -z "$CONTAINER_NODE" ]; then
    echo "❌ Container VMID $VMID not found on any node"
    exit 1
fi

echo "Container found on node: $CONTAINER_NODE"
echo ""

# Get container network config
echo "Container network configuration:"
ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "pvesh get /nodes/$CONTAINER_NODE/lxc/$VMID/config --output-format json-pretty 2>/dev/null | grep net0" || echo "Could not retrieve config"
echo ""

# Try to get actual IP from container
echo "Attempting to get actual IP address from container..."
ACTUAL_IP=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "pct exec $VMID -- ip -4 addr show eth0 2>/dev/null | grep 'inet ' | awk '{print \$2}' | cut -d'/' -f1" 2>&1 || echo "")

if [ -n "$ACTUAL_IP" ] && [ "$ACTUAL_IP" != "" ]; then
    echo "✅ Actual IP address: $ACTUAL_IP"
    echo ""
    echo "Expected IP (from config): 192.168.11.140"
    if [ "$ACTUAL_IP" = "192.168.11.140" ]; then
        echo "✅ IP matches expected configuration"
    else
        echo "⚠️  IP does NOT match expected configuration!"
        echo ""
        echo "The container is using DHCP and got a different IP than expected."
        echo "You may need to:"
        echo "  1. Configure static IP reservation in DHCP server"
        echo "  2. Or update scripts to use actual IP: $ACTUAL_IP"
    fi
else
    echo "⚠️  Could not retrieve IP from container"
    echo "Container may not be running or network not configured"
fi

echo ""
echo "Configuration references:"
echo "  - network.conf: PUBLIC_START_IP=\"192.168.11.140\""
echo "  - deploy-explorer.sh: ip_octet=140 → 192.168.11.140"
echo "  - Container uses: ip=dhcp (may get different IP)"

