#!/bin/bash
# Monitor all RPC Translator services and supporting services
# Usage: ./scripts/monitor-services.sh

set -e

echo "═══════════════════════════════════════════════════════════════"
echo "🔍 RPC TRANSLATOR 138 - SERVICE MONITORING"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Supporting Services
echo "Supporting Services:"
echo "───────────────────────────────────────────────────────────────"

echo -n "Redis (192.168.11.110:6379): "
if timeout 2 bash -c "echo > /dev/tcp/192.168.11.110/6379" 2>/dev/null; then
    echo "✅ Reachable"
else
    echo "❌ Not reachable"
fi

echo -n "Web3Signer (192.168.11.111:9000): "
WEB3SIGNER_STATUS=$(curl -s -m 2 http://192.168.11.111:9000/upcheck 2>/dev/null || echo "ERROR")
if [ "$WEB3SIGNER_STATUS" = "OK" ]; then
    echo "✅ Operational"
else
    echo "❌ Not responding"
fi

echo -n "Vault (192.168.11.112:8200): "
VAULT_STATUS=$(curl -s -m 2 http://192.168.11.112:8200/v1/sys/health 2>/dev/null | grep -o '"initialized":[^,]*' || echo "ERROR")
if [ "$VAULT_STATUS" != "ERROR" ]; then
    echo "✅ Operational"
else
    echo "❌ Not responding"
fi

echo ""
echo "Translator Services:"
echo "───────────────────────────────────────────────────────────────"

for IP in 192.168.11.240 192.168.11.241 192.168.11.242; do
    LAST_OCTET="${IP##*.}"
    VMID="24$((LAST_OCTET - 240))"
    echo ""
    echo "VMID $VMID ($IP):"
    
    # Service status
    echo -n "  Service Status: "
    SERVICE_STATUS=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$IP "systemctl is-active rpc-translator-138.service" 2>/dev/null || echo "ERROR")
    if [ "$SERVICE_STATUS" = "active" ]; then
        echo "✅ Active"
    else
        echo "❌ $SERVICE_STATUS"
    fi
    
    # Health check
    echo -n "  Health Check: "
    HEALTH=$(curl -s -m 2 http://$IP:9545/health 2>/dev/null || echo "ERROR")
    if echo "$HEALTH" | grep -q '"status":"ok"'; then
        UPSTREAM_HEALTHY=$(echo "$HEALTH" | grep -o '"healthyCount":[0-9]*' | grep -o '[0-9]*')
        UPSTREAM_TOTAL=$(echo "$HEALTH" | grep -o '"totalCount":[0-9]*' | grep -o '[0-9]*')
        if [ "$UPSTREAM_HEALTHY" = "$UPSTREAM_TOTAL" ] && [ -n "$UPSTREAM_HEALTHY" ]; then
            echo "✅ Healthy (Upstream: $UPSTREAM_HEALTHY/$UPSTREAM_TOTAL)"
        else
            echo "⚠️  Degraded (Upstream: $UPSTREAM_HEALTHY/$UPSTREAM_TOTAL)"
        fi
    else
        echo "❌ Not responding"
    fi
    
    # RPC test
    echo -n "  RPC Test: "
    RPC_RESPONSE=$(curl -s -m 2 -X POST http://$IP:9545 -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' 2>/dev/null || echo "ERROR")
    if echo "$RPC_RESPONSE" | grep -q '"result"'; then
        CHAIN_ID=$(echo "$RPC_RESPONSE" | grep -o '"result":"[^"]*"' | cut -d'"' -f4)
        echo "✅ Working (ChainID: $CHAIN_ID)"
    elif echo "$RPC_RESPONSE" | grep -q '"error"'; then
        echo "⚠️  Error response"
    else
        echo "❌ No response"
    fi
done

echo ""
echo "═══════════════════════════════════════════════════════════════"
