#!/bin/bash
# Check all services status - Complete verification
# Usage: ./scripts/check-all-status.sh

set -e

PROXMOX_HOST="192.168.11.11"
WEB3SIGNER_IP="192.168.11.111"
REDIS_IP="192.168.11.110"
VAULT_IP="192.168.11.112"

echo "═══════════════════════════════════════════════════════════════"
echo "🔍 COMPLETE STATUS CHECK - RPC TRANSLATOR 138"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Supporting Services
echo "═══════════════════════════════════════════════════════════════"
echo "📦 SUPPORTING SERVICES"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Redis
echo "1. Redis (192.168.11.110:6379):"
REDIS_PING=$(ssh root@$PROXMOX_HOST "pct exec 106 -- redis-cli ping 2>/dev/null" 2>&1 || echo "not responding")
if [ "$REDIS_PING" = "PONG" ]; then
    echo "   ✅ Running (PONG)"
else
    echo "   ❌ Not responding: $REDIS_PING"
fi

# Web3Signer
echo ""
echo "2. Web3Signer (192.168.11.111:9000):"
WEB3SIGNER_HEALTH=$(curl -s -m 5 http://$WEB3SIGNER_IP:9000/upcheck 2>&1 || echo "not responding")
if [ "$WEB3SIGNER_HEALTH" = "OK" ]; then
    echo "   ✅ Health: OK"
    
    # Check service status
    SERVICE_STATUS=$(ssh root@$PROXMOX_HOST "pct exec 107 -- systemctl is-active web3signer.service" 2>&1 || echo "unknown")
    echo "   Service: $SERVICE_STATUS"
    
    # Check keys
    KEYS=$(curl -s http://$WEB3SIGNER_IP:9000/api/v1/eth1/publicKeys 2>&1 || echo "[]")
    if [ "$KEYS" != "[]" ] && [ -n "$KEYS" ]; then
        KEY_COUNT=$(echo "$KEYS" | jq '. | length' 2>/dev/null || echo "0")
        if [ "$KEY_COUNT" -gt 0 ]; then
            echo "   ✅ Keys loaded: $KEY_COUNT"
            echo "$KEYS" | jq -r '.[]' | while read addr; do
                echo "      - $addr"
            done
        else
            echo "   ⚠️  Keys: Empty array"
        fi
    else
        echo "   ⚠️  Keys: Not loaded"
    fi
else
    echo "   ❌ Health: $WEB3SIGNER_HEALTH"
fi

# Vault
echo ""
echo "3. Vault (192.168.11.112:8200):"
VAULT_HEALTH=$(curl -s -m 5 http://$VAULT_IP:8200/v1/sys/health 2>&1 | jq -r '.status' 2>/dev/null || echo "not responding")
if [ "$VAULT_HEALTH" = "active" ] || [ "$VAULT_HEALTH" = "standby" ]; then
    echo "   ✅ Status: $VAULT_HEALTH"
else
    echo "   ⚠️  Status: $VAULT_HEALTH"
fi

# Translator Services
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "🌐 TRANSLATOR SERVICES"
echo "═══════════════════════════════════════════════════════════════"
echo ""

for IP in 192.168.11.240 192.168.11.241 192.168.11.242; do
    VMID=$(echo "$IP" | awk -F'.' '{print $4}')
    echo "VMID $VMID ($IP):"
    
    # Service status
    SERVICE_STATUS=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@$IP "systemctl is-active rpc-translator-138.service" 2>/dev/null || echo "unknown")
    if [ "$SERVICE_STATUS" = "active" ]; then
        echo "   ✅ Service: Active"
    else
        echo "   ❌ Service: $SERVICE_STATUS"
    fi
    
    # Health check
    HEALTH=$(curl -s -m 5 http://$IP:9545/health 2>&1 | jq -r '.status' 2>/dev/null || echo "not responding")
    if [ "$HEALTH" = "ok" ] || [ "$HEALTH" = "degraded" ]; then
        echo "   ✅ Health: $HEALTH"
    else
        echo "   ⚠️  Health: $HEALTH"
    fi
    
    # RPC test
    RPC_RESULT=$(curl -s -m 5 -X POST http://$IP:9545 \
        -H 'Content-Type: application/json' \
        -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' 2>&1 | jq -r '.result' 2>/dev/null || echo "error")
    if [ "$RPC_RESULT" != "error" ] && [ -n "$RPC_RESULT" ]; then
        echo "   ✅ RPC: Working (ChainID: $RPC_RESULT)"
    else
        echo "   ⚠️  RPC: Not responding"
    fi
    
    # Allowlist
    ALLOWLIST=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@$IP "grep '^WALLET_ALLOWLIST=' /opt/rpc-translator-138/.env 2>/dev/null | cut -d'=' -f2-" || echo "")
    if [ -n "$ALLOWLIST" ] && [ "$ALLOWLIST" != "" ]; then
        COUNT=$(echo "$ALLOWLIST" | tr ',' '\n' | wc -l)
        echo "   ✅ Allowlist: $COUNT address(es) configured"
    else
        echo "   ⚠️  Allowlist: Not configured"
    fi
    
    echo ""
done

# Summary
echo "═══════════════════════════════════════════════════════════════"
echo "📊 SUMMARY"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Count services
REDIS_OK=$([ "$REDIS_PING" = "PONG" ] && echo "1" || echo "0")
WEB3SIGNER_OK=$([ "$WEB3SIGNER_HEALTH" = "OK" ] && echo "1" || echo "0")
VAULT_OK=$([ "$VAULT_HEALTH" = "active" ] || [ "$VAULT_HEALTH" = "standby" ] && echo "1" || echo "0")

TRANSLATOR_COUNT=0
for IP in 192.168.11.240 192.168.11.241 192.168.11.242; do
    SERVICE_STATUS=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@$IP "systemctl is-active rpc-translator-138.service" 2>/dev/null || echo "unknown")
    if [ "$SERVICE_STATUS" = "active" ]; then
        TRANSLATOR_COUNT=$((TRANSLATOR_COUNT + 1))
    fi
done

echo "Supporting Services:"
echo "  Redis: $([ "$REDIS_OK" = "1" ] && echo "✅" || echo "❌")"
echo "  Web3Signer: $([ "$WEB3SIGNER_OK" = "1" ] && echo "✅" || echo "❌")"
echo "  Vault: $([ "$VAULT_OK" = "1" ] && echo "✅" || echo "❌")"
echo ""
echo "Translator Services: $TRANSLATOR_COUNT/3 active"
echo ""

# Overall status
TOTAL_OK=$((REDIS_OK + WEB3SIGNER_OK + VAULT_OK))
if [ "$TOTAL_OK" = "3" ] && [ "$TRANSLATOR_COUNT" = "3" ]; then
    echo "🎉 ALL SERVICES OPERATIONAL"
else
    echo "⚠️  Some services need attention"
fi

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