#!/bin/bash
# get-smom-vm-ips.sh
# Get all SMOM-DBIS-138 VM IP addresses and export to SMOM-DBIS-138 project

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
SMOM_PROJECT="${SMOM_PROJECT:-$HOME/projects/smom-dbis-138}"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

log() {
    echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*"
}

log_success() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] ✅${NC} $*"
}

log_warning() {
    echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] ⚠️${NC} $*"
}

log_error() {
    echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ❌${NC} $*"
}

get_vm_ip() {
    local vm_name=$1
    local ip
    
    ip=$(kubectl get proxmoxvm "${vm_name}" -n default -o jsonpath='{.status.ipAddress}' 2>/dev/null || echo "")
    
    if [ -z "${ip}" ] || [ "${ip}" = "<none>" ]; then
        echo ""
        return 1
    fi
    
    echo "${ip}"
}

main() {
    log "=========================================="
    log "SMOM-DBIS-138 VM IP Address Collector"
    log "=========================================="
    log ""
    
    local output_file="${PROJECT_ROOT}/smom-vm-ips.txt"
    local smom_config_file="${SMOM_PROJECT}/config/vm-ips.txt"
    
    log "Collecting VM IP addresses..."
    log ""
    
    # Infrastructure VMs
    log "Infrastructure VMs:"
    {
        echo "# SMOM-DBIS-138 VM IP Addresses"
        echo "# Generated: $(date -Iseconds)"
        echo "#"
        echo "# Infrastructure VMs"
        echo ""
        
        local nginx_ip sentry_ip
        nginx_ip=$(get_vm_ip "nginx-proxy-vm" 2>/dev/null || echo "")
        if [ -n "${nginx_ip}" ]; then
            echo "NGINX_PROXY_IP=${nginx_ip}"
            log_success "nginx-proxy-vm: ${nginx_ip}"
        else
            echo "# NGINX_PROXY_IP= (not yet assigned)"
            log_warning "nginx-proxy-vm: IP not yet assigned"
        fi
        
        local tunnel_ip
        tunnel_ip=$(get_vm_ip "cloudflare-tunnel-vm" 2>/dev/null || echo "")
        if [ -n "${tunnel_ip}" ]; then
            echo "CLOUDFLARE_TUNNEL_IP=${tunnel_ip}"
            log_success "cloudflare-tunnel-vm: ${tunnel_ip}"
        else
            echo "# CLOUDFLARE_TUNNEL_IP= (not yet assigned)"
            log_warning "cloudflare-tunnel-vm: IP not yet assigned"
        fi
        
        echo ""
        echo "# Application VMs"
        echo ""
        echo "# Validators"
        
        # Validators
        for i in 01 02 03 04; do
            local ip
            ip=$(get_vm_ip "smom-validator-${i}" 2>/dev/null || echo "")
            if [ -n "${ip}" ]; then
                echo "VALIDATOR_${i}_IP=${ip}"
                log_success "smom-validator-${i}: ${ip}"
            else
                echo "# VALIDATOR_${i}_IP= (not yet assigned)"
                log_warning "smom-validator-${i}: IP not yet assigned"
            fi
        done
        
        echo ""
        echo "# Sentries"
        
        # Sentries
        for i in 01 02 03 04; do
            local ip
            ip=$(get_vm_ip "smom-sentry-${i}" 2>/dev/null || echo "")
            if [ -n "${ip}" ]; then
                echo "SENTRY_${i}_IP=${ip}"
                log_success "smom-sentry-${i}: ${ip}"
            else
                echo "# SENTRY_${i}_IP= (not yet assigned)"
                log_warning "smom-sentry-${i}: IP not yet assigned"
            fi
        done
        
        echo ""
        echo "# RPC Nodes"
        
        # RPC Nodes
        for i in 01 02 03 04; do
            local ip
            ip=$(get_vm_ip "smom-rpc-node-${i}" 2>/dev/null || echo "")
            if [ -n "${ip}" ]; then
                echo "RPC_NODE_${i}_IP=${ip}"
                log_success "smom-rpc-node-${i}: ${ip}"
            else
                echo "# RPC_NODE_${i}_IP= (not yet assigned)"
                log_warning "smom-rpc-node-${i}: IP not yet assigned"
            fi
        done
        
        echo ""
        echo "# Services"
        
        # Services
        local services_ip
        services_ip=$(get_vm_ip "smom-services" 2>/dev/null || echo "")
        if [ -n "${services_ip}" ]; then
            echo "SERVICES_IP=${services_ip}"
            log_success "smom-services: ${services_ip}"
        else
            echo "# SERVICES_IP= (not yet assigned)"
            log_warning "smom-services: IP not yet assigned"
        fi
        
        # Blockscout
        local blockscout_ip
        blockscout_ip=$(get_vm_ip "smom-blockscout" 2>/dev/null || echo "")
        if [ -n "${blockscout_ip}" ]; then
            echo "BLOCKSCOUT_IP=${blockscout_ip}"
            log_success "smom-blockscout: ${blockscout_ip}"
        else
            echo "# BLOCKSCOUT_IP= (not yet assigned)"
            log_warning "smom-blockscout: IP not yet assigned"
        fi
        
        # Monitoring
        local monitoring_ip
        monitoring_ip=$(get_vm_ip "smom-monitoring" 2>/dev/null || echo "")
        if [ -n "${monitoring_ip}" ]; then
            echo "MONITORING_IP=${monitoring_ip}"
            log_success "smom-monitoring: ${monitoring_ip}"
        else
            echo "# MONITORING_IP= (not yet assigned)"
            log_warning "smom-monitoring: IP not yet assigned"
        fi
        
        # Management
        local management_ip
        management_ip=$(get_vm_ip "smom-management" 2>/dev/null || echo "")
        if [ -n "${management_ip}" ]; then
            echo "MANAGEMENT_IP=${management_ip}"
            log_success "smom-management: ${management_ip}"
        else
            echo "# MANAGEMENT_IP= (not yet assigned)"
            log_warning "smom-management: IP not yet assigned"
        fi
        
    } > "${output_file}"
    
    log ""
    log_success "VM IPs saved to: ${output_file}"
    
    # Copy to SMOM-DBIS-138 project if it exists
    if [ -d "${SMOM_PROJECT}" ]; then
        mkdir -p "${SMOM_PROJECT}/config"
        cp "${output_file}" "${smom_config_file}"
        log_success "VM IPs copied to: ${smom_config_file}"
        log ""
        log "To use in SMOM-DBIS-138 project:"
        log "  cd ${SMOM_PROJECT}"
        log "  source config/vm-ips.txt"
    else
        log_warning "SMOM-DBIS-138 project not found at: ${SMOM_PROJECT}"
        log "  Set SMOM_PROJECT environment variable to override"
    fi
    
    log ""
    log "=========================================="
    log_success "Complete!"
    log ""
}

main "$@"

