#!/usr/bin/env bash

# Get VM IP addresses
# This script retrieves and displays VM IP addresses

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# Configuration
RESOURCE_GROUP="${RESOURCE_GROUP:-defi-oracle-mainnet-rg}"


log_success "Getting VM IP addresses..."

# Get all VMs
VMS=$(az vm list --resource-group "$RESOURCE_GROUP" --show-details --query "[].{Name:name, PrivateIP:privateIps, PublicIP:publicIps, PowerState:powerState}" -o json 2>/dev/null || echo "[]")

if [ "$VMS" == "[]" ]; then
    log_warn "No VMs found in resource group: $RESOURCE_GROUP"
    exit 0
fi

log_warn "=== Validator VMs ==="
echo "$VMS" | jq -r '.[] | select(.Name | contains("validator")) | "\(.Name):\n  Private IP: \(.PrivateIP)\n  Public IP: \(.PublicIP // "N/A")\n  Status: \(.PowerState)\n"'

log_warn "=== Sentry VMs ==="
echo "$VMS" | jq -r '.[] | select(.Name | contains("sentry")) | "\(.Name):\n  Private IP: \(.PrivateIP)\n  Public IP: \(.PublicIP // "N/A")\n  Status: \(.PowerState)\n"'

log_warn "=== RPC VMs ==="
echo "$VMS" | jq -r '.[] | select(.Name | contains("rpc")) | "\(.Name):\n  Private IP: \(.PrivateIP)\n  Public IP: \(.PublicIP // "N/A")\n  Status: \(.PowerState)\n"'

log_success "VM IP addresses retrieved!"

