#!/bin/bash
# verify-deployment.sh
# Verify SMOM-DBIS-138 deployment status

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 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} $*"
}

check_vm_status() {
    local vm_name=$1
    local vm_id
    local state
    local ip
    
    vm_id=$(kubectl get proxmoxvm "${vm_name}" -n default -o jsonpath='{.status.vmId}' 2>/dev/null || echo "<none>")
    state=$(kubectl get proxmoxvm "${vm_name}" -n default -o jsonpath='{.status.state}' 2>/dev/null || echo "<none>")
    ip=$(kubectl get proxmoxvm "${vm_name}" -n default -o jsonpath='{.status.ipAddress}' 2>/dev/null || echo "<none>")
    
    if [ "${vm_id}" != "<none>" ] && [ -n "${vm_id}" ]; then
        if [ "${state}" = "running" ]; then
            log_success "${vm_name}: VMID=${vm_id}, State=${state}, IP=${ip}"
            return 0
        else
            log_warning "${vm_name}: VMID=${vm_id}, State=${state}, IP=${ip}"
            return 1
        fi
    else
        log_error "${vm_name}: Not yet created"
        return 1
    fi
}

main() {
    log "=========================================="
    log "SMOM-DBIS-138 Deployment Verification"
    log "=========================================="
    log ""
    
    local total_vms=0
    local running_vms=0
    local stopped_vms=0
    local pending_vms=0
    
    # Infrastructure VMs
    log "Infrastructure VMs:"
    log "-------------------"
    
    if check_vm_status "nginx-proxy-vm"; then
        running_vms=$((running_vms + 1))
    else
        stopped_vms=$((stopped_vms + 1))
    fi
    total_vms=$((total_vms + 1))
    
    if check_vm_status "cloudflare-tunnel-vm"; then
        running_vms=$((running_vms + 1))
    else
        stopped_vms=$((stopped_vms + 1))
    fi
    total_vms=$((total_vms + 1))
    
    log ""
    
    # Application VMs
    log "Application VMs:"
    log "----------------"
    
    # Validators
    for i in 01 02 03 04; do
        if check_vm_status "smom-validator-${i}"; then
            running_vms=$((running_vms + 1))
        else
            stopped_vms=$((stopped_vms + 1))
        fi
        total_vms=$((total_vms + 1))
    done
    
    # Sentries
    for i in 01 02 03 04; do
        if check_vm_status "smom-sentry-${i}"; then
            running_vms=$((running_vms + 1))
        else
            stopped_vms=$((stopped_vms + 1))
        fi
        total_vms=$((total_vms + 1))
    done
    
    # RPC Nodes
    for i in 01 02 03 04; do
        if check_vm_status "smom-rpc-node-${i}"; then
            running_vms=$((running_vms + 1))
        else
            stopped_vms=$((stopped_vms + 1))
        fi
        total_vms=$((total_vms + 1))
    done
    
    # Services
    if check_vm_status "smom-services"; then
        running_vms=$((running_vms + 1))
    else
        stopped_vms=$((stopped_vms + 1))
    fi
    total_vms=$((total_vms + 1))
    
    # Blockscout
    if check_vm_status "smom-blockscout"; then
        running_vms=$((running_vms + 1))
    else
        stopped_vms=$((stopped_vms + 1))
    fi
    total_vms=$((total_vms + 1))
    
    # Monitoring
    if check_vm_status "smom-monitoring"; then
        running_vms=$((running_vms + 1))
    else
        stopped_vms=$((stopped_vms + 1))
    fi
    total_vms=$((total_vms + 1))
    
    # Management
    if check_vm_status "smom-management"; then
        running_vms=$((running_vms + 1))
    else
        stopped_vms=$((stopped_vms + 1))
    fi
    total_vms=$((total_vms + 1))
    
    log ""
    log "=========================================="
    log "Summary:"
    log "  Total VMs: ${total_vms}"
    log "  Running: ${running_vms}"
    log "  Stopped/Pending: ${stopped_vms}"
    log "=========================================="
    log ""
    
    if [ ${running_vms} -eq ${total_vms} ]; then
        log_success "All VMs are running!"
    elif [ ${running_vms} -gt 0 ]; then
        log_warning "Some VMs are still starting. This is normal during initial deployment."
    else
        log_warning "VMs are being created. Please wait and check again."
    fi
    
    log ""
    log "Next steps:"
    log "1. Wait for all VMs to be in 'running' state"
    log "2. Configure infrastructure VMs (Nginx Proxy, Cloudflare Tunnel)"
    log "3. Configure application VMs with SMOM-DBIS-138 software"
    log "4. Verify connectivity between components"
    log ""
}

main "$@"
