#!/bin/bash
# Complete Web3Signer verification script
# Usage: ./scripts/verify-web3signer-complete.sh

set -e

WEB3SIGNER_URL="http://192.168.11.111:9000"

echo "═══════════════════════════════════════════════════════════════"
echo "🔍 COMPLETE WEB3SIGNER VERIFICATION"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Service status
echo "1. Service Status:"
SERVICE_STATUS=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@192.168.11.111 "systemctl is-active web3signer.service" 2>/dev/null || echo "ERROR")
if [ "$SERVICE_STATUS" = "active" ]; then
    echo "   ✅ Active"
else
    echo "   ❌ $SERVICE_STATUS"
    exit 1
fi

# Health check
echo ""
echo "2. Health Check:"
HEALTH=$(curl -s -m 3 "$WEB3SIGNER_URL/upcheck" 2>/dev/null || echo "ERROR")
if [ "$HEALTH" = "OK" ]; then
    echo "   ✅ Healthy"
else
    echo "   ❌ Not responding: $HEALTH"
    exit 1
fi

# Java version
echo ""
echo "3. Java Version:"
JAVA_VERSION=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@192.168.11.111 "java -version 2>&1 | head -1" 2>/dev/null || echo "")
if echo "$JAVA_VERSION" | grep -q "21"; then
    echo "   ✅ Java 21 installed"
    echo "      $JAVA_VERSION"
else
    echo "   ⚠️  Java version may be incorrect: $JAVA_VERSION"
fi

# Web3Signer version
echo ""
echo "4. Web3Signer Version:"
WEB3SIGNER_VER=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@192.168.11.111 "/opt/web3signer-23.10.0/bin/web3signer --version 2>&1 | head -1" 2>/dev/null || echo "")
echo "   $WEB3SIGNER_VER"

# Loaded keys
echo ""
echo "5. Loaded Keys:"
KEYS=$(curl -s -m 3 "$WEB3SIGNER_URL/api/v1/eth1/publicKeys" 2>/dev/null || echo "[]")
KEY_COUNT=$(echo "$KEYS" | jq '. | length' 2>/dev/null || echo "0")
if [ "$KEY_COUNT" -gt 0 ]; then
    echo "   ✅ $KEY_COUNT key(s) loaded:"
    echo "$KEYS" | jq -r '.[]' 2>/dev/null | while read key; do
        echo "      - $key"
    done
else
    echo "   ⚠️  No keys loaded"
    echo ""
    echo "   To load keys, run:"
    echo "     ./scripts/setup-complete.sh 3 mypassword"
fi

# Translator connectivity
echo ""
echo "6. Translator Connectivity:"
for IP in 192.168.11.240 192.168.11.241 192.168.11.242; do
    CONFIG=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@$IP "grep WEB3SIGNER_URL /opt/rpc-translator-138/.env" 2>/dev/null || echo "")
    if echo "$CONFIG" | grep -q "192.168.11.111"; then
        echo "   ✅ $IP: Configured correctly"
    else
        echo "   ⚠️  $IP: Configuration issue"
    fi
done

echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "📋 SUMMARY"
echo "═══════════════════════════════════════════════════════════════"
echo ""

if [ "$SERVICE_STATUS" = "active" ] && [ "$HEALTH" = "OK" ]; then
    echo "✅ Web3Signer is FULLY FUNCTIONAL"
    echo ""
    if [ "$KEY_COUNT" -gt 0 ]; then
        echo "✅ Ready for transaction signing"
        echo "   $KEY_COUNT key(s) loaded and ready"
    else
        echo "⚠️  Ready for transaction signing (once keys are loaded)"
        echo ""
        echo "Next step: Load signing keys"
        echo "  Run: ./scripts/setup-complete.sh 3 mypassword"
    fi
else
    echo "❌ Web3Signer has issues"
    echo "   Service: $SERVICE_STATUS"
    echo "   Health: $HEALTH"
fi

echo ""
