#!/usr/bin/env bash
# Check Infrastructure Deployment Status

set -e

cd "$(dirname "$0")/../.."

# Color codes

echo "==================================================================="
echo "  CHAIN-138 INFRASTRUCTURE DEPLOYMENT STATUS"
echo "==================================================================="

# Check Azure
log_info "Azure Resources:"
RG_NAME=$(cd terraform && terraform output -raw resource_group_name 2>/dev/null || echo "az-p-we-rg-comp-001")
CLUSTER_NAME=$(cd terraform && terraform output -raw cluster_name 2>/dev/null || echo "az-p-we-aks-main")

if az group show --name "$RG_NAME" &> /dev/null; then
    log_success "✅ Resource Group: $RG_NAME"
else
    log_error "❌ Resource Group not found: $RG_NAME"
fi

if az aks show --resource-group "$RG_NAME" --name "$CLUSTER_NAME" &> /dev/null; then
    log_success "✅ AKS Cluster: $CLUSTER_NAME"
    STATUS=$(az aks show --resource-group "$RG_NAME" --name "$CLUSTER_NAME" --query powerState.code -o tsv 2>/dev/null || echo "Unknown")
    echo "   Power State: $STATUS"
else
    log_error "❌ AKS Cluster not found: $CLUSTER_NAME"
fi

# Check Kubernetes
log_info "Kubernetes:"
if kubectl cluster-info &> /dev/null 2>&1; then
    log_success "✅ Cluster accessible"
    NODES=$(kubectl get nodes --no-headers 2>/dev/null | wc -l)
    echo "   Nodes: $NODES"
else
    log_error "❌ Cluster not accessible"
fi

# Check Namespaces
log_info "Namespaces:"
if kubectl get namespace besu-network &> /dev/null 2>&1; then
    log_success "✅ besu-network"
    PODS=$(kubectl get pods -n besu-network --no-headers 2>/dev/null | wc -l)
    READY=$(kubectl get pods -n besu-network --no-headers 2>/dev/null | grep -c "Running\|Completed" || echo "0")
    echo "   Pods: $READY/$PODS ready"
else
    log_warn "⚠️  besu-network not found"
fi

if kubectl get namespace monitoring &> /dev/null 2>&1; then
    log_success "✅ monitoring"
    PODS=$(kubectl get pods -n monitoring --no-headers 2>/dev/null | wc -l)
    READY=$(kubectl get pods -n monitoring --no-headers 2>/dev/null | grep -c "Running\|Completed" || echo "0")
    echo "   Pods: $READY/$PODS ready"
else
    log_warn "⚠️  monitoring not found"
fi

# Check Helm Releases
log_info "Helm Releases:"
if helm list -n besu-network 2>/dev/null | grep -q besu-validators; then
    log_success "✅ besu-validators"
else
    log_warn "⚠️  besu-validators not deployed"
fi

if helm list -n besu-network 2>/dev/null | grep -q besu-sentries; then
    log_success "✅ besu-sentries"
else
    log_warn "⚠️  besu-sentries not deployed"
fi

if helm list -n besu-network 2>/dev/null | grep -q besu-rpc; then
    log_success "✅ besu-rpc"
else
    log_warn "⚠️  besu-rpc not deployed"
fi

# Check RPC Endpoint
log_info "RPC Endpoint:"
RPC_SVC=$(kubectl get svc -n besu-network -l app=besu-rpc -o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
if [ -n "$RPC_SVC" ]; then
    RPC_IP=$(kubectl get svc -n besu-network "$RPC_SVC" -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null || echo "")
    if [ -n "$RPC_IP" ]; then
        log_success "✅ RPC Service: $RPC_SVC"
        echo "   Endpoint: http://$RPC_IP:8545"
    else
        log_warn "⚠️  RPC Service exists but IP not assigned"
    fi
else
    log_warn "⚠️  RPC Service not found"
fi

echo "==================================================================="
