#!/usr/bin/env bash
# Verify all Chain-138 services

set -e

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

# Color codes

echo "==================================================================="
echo "  CHAIN-138 SERVICES VERIFICATION"
echo "==================================================================="

# Load environment variables
if [ -f .env ]; then
    source .env 2>/dev/null || true
fi

ERRORS=0
WARNINGS=0
SUCCESS=0

check_service() {
    local name=$1
    local status=$2
    local details=$3
    
    if [ "$status" = "success" ]; then
        echo -e "  ${GREEN}✅ $name${NC}"
        SUCCESS=$((SUCCESS + 1))
    elif [ "$status" = "warning" ]; then
        echo -e "  ${YELLOW}⚠️  $name${NC}"
        WARNINGS=$((WARNINGS + 1))
    else
        echo -e "  ${RED}❌ $name${NC}"
        ERRORS=$((ERRORS + 1))
    fi
    
    if [ -n "$details" ]; then
        echo "    $details"
    fi
}

log_info "1. Blockchain Services"

# Check Besu validators
if command -v kubectl &> /dev/null && kubectl cluster-info &> /dev/null 2>&1; then
    VALIDATOR_PODS=$(kubectl get pods -n besu-network -l app=besu-validator 2>/dev/null | grep -v NAME | wc -l)
    if [ "$VALIDATOR_PODS" -gt 0 ]; then
        RUNNING=$(kubectl get pods -n besu-network -l app=besu-validator 2>/dev/null | grep Running | wc -l)
        check_service "Besu Validators" "success" "$RUNNING/$VALIDATOR_PODS running"
    else
        check_service "Besu Validators" "warning" "No validator pods found"
    fi
    
    # Check sentries
    SENTRY_PODS=$(kubectl get pods -n besu-network -l app=besu-sentry 2>/dev/null | grep -v NAME | wc -l)
    if [ "$SENTRY_PODS" -gt 0 ]; then
        RUNNING=$(kubectl get pods -n besu-network -l app=besu-sentry 2>/dev/null | grep Running | wc -l)
        check_service "Besu Sentries" "success" "$RUNNING/$SENTRY_PODS running"
    else
        check_service "Besu Sentries" "warning" "No sentry pods found"
    fi
    
    # Check RPC nodes
    RPC_PODS=$(kubectl get pods -n besu-network -l app=besu-rpc 2>/dev/null | grep -v NAME | wc -l)
    if [ "$RPC_PODS" -gt 0 ]; then
        RUNNING=$(kubectl get pods -n besu-network -l app=besu-rpc 2>/dev/null | grep Running | wc -l)
        check_service "Besu RPC Nodes" "success" "$RUNNING/$RPC_PODS running"
    else
        check_service "Besu RPC Nodes" "warning" "No RPC pods found"
    fi
else
    check_service "Kubernetes Access" "warning" "kubectl not available or cluster not accessible"
fi

log_info "2. Monitoring Services"

# Check Prometheus
if command -v kubectl &> /dev/null && kubectl cluster-info &> /dev/null 2>&1; then
    if kubectl get pods -n monitoring -l app=prometheus 2>/dev/null | grep -q Running; then
        check_service "Prometheus" "success" "Running"
    else
        check_service "Prometheus" "warning" "Not running or not found"
    fi
    
    # Check Grafana
    if kubectl get pods -n monitoring -l app=grafana 2>/dev/null | grep -q Running; then
        check_service "Grafana" "success" "Running"
    else
        check_service "Grafana" "warning" "Not running or not found"
    fi
else
    check_service "Monitoring Services" "warning" "Cannot check (kubectl not available)"
fi

log_info "3. Explorer Services"

# Check Blockscout
if command -v kubectl &> /dev/null && kubectl cluster-info &> /dev/null 2>&1; then
    if kubectl get pods -n besu-network -l app=blockscout 2>/dev/null | grep -q Running; then
        check_service "Blockscout" "success" "Running"
    else
        check_service "Blockscout" "warning" "Not running or not found"
    fi
else
    check_service "Blockscout" "warning" "Cannot check (kubectl not available)"
fi

log_info "4. Network Services"

# Check RPC endpoint
if [ -n "$CHAIN138_RPC_URL" ]; then
    if curl -s --max-time 5 "$CHAIN138_RPC_URL" -X POST -H "Content-Type: application/json" \
        -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' 2>/dev/null | grep -q "result"; then
        check_service "RPC Endpoint" "success" "$CHAIN138_RPC_URL"
    else
        check_service "RPC Endpoint" "error" "$CHAIN138_RPC_URL (not accessible)"
    fi
else
    check_service "RPC Endpoint" "error" "Not configured"
fi

# Check WebSocket endpoint (if configured)
if [ -n "$CHAIN138_WS_URL" ]; then
    check_service "WebSocket Endpoint" "success" "Configured: $CHAIN138_WS_URL"
else
    check_service "WebSocket Endpoint" "warning" "Not configured"
fi

log_info "5. Configuration Files"

# Check Kubernetes manifests
if [ -d "k8s" ]; then
    MANIFEST_COUNT=$(find k8s -name "*.yaml" -o -name "*.yml" 2>/dev/null | wc -l)
    check_service "Kubernetes Manifests" "success" "$MANIFEST_COUNT files found"
else
    check_service "Kubernetes Manifests" "warning" "k8s directory not found"
fi

# Check Helm charts
if [ -d "helm" ]; then
    CHART_COUNT=$(find helm -name "Chart.yaml" 2>/dev/null | wc -l)
    check_service "Helm Charts" "success" "$CHART_COUNT charts found"
else
    check_service "Helm Charts" "warning" "helm directory not found"
fi

# Check Terraform
if [ -d "terraform" ]; then
    TF_FILES=$(find terraform -name "*.tf" 2>/dev/null | wc -l)
    check_service "Terraform Config" "success" "$TF_FILES files found"
else
    check_service "Terraform Config" "warning" "terraform directory not found"
fi

echo "==================================================================="
log_info "SUMMARY"
echo "==================================================================="
echo "  ✅ Successful: $SUCCESS"
echo "  ⚠️  Warnings: $WARNINGS"
echo "  ❌ Errors: $ERRORS"

if [ $ERRORS -eq 0 ]; then
    log_success "✅ All services verified!"
    exit 0
else
    log_error "❌ Some services have errors"
    exit 1
fi
