#!/bin/bash
# Test Web3Signer integration with translator
# Usage: ./scripts/test-web3signer-integration.sh [translator-ip]

set -e

TRANSLATOR_IP="${1:-192.168.11.240}"
WEB3SIGNER_URL="http://192.168.11.111:9000"

echo "═══════════════════════════════════════════════════════════════"
echo "🔍 TESTING WEB3SIGNER INTEGRATION"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "Translator: $TRANSLATOR_IP"
echo "Web3Signer: $WEB3SIGNER_URL"
echo ""

# 1. Check Web3Signer health
echo "1. Web3Signer Health:"
WEB3SIGNER_HEALTH=$(curl -s -m 3 "$WEB3SIGNER_URL/upcheck" 2>/dev/null || echo "ERROR")
if [ "$WEB3SIGNER_HEALTH" = "OK" ]; then
    echo "   ✅ Web3Signer is healthy"
else
    echo "   ❌ Web3Signer is not responding"
    exit 1
fi

# 2. Check for loaded keys
echo ""
echo "2. Loaded Keys:"
KEYS=$(curl -s -m 3 "$WEB3SIGNER_URL/api/v1/eth1/publicKeys" 2>/dev/null || echo "[]")
if [ "$KEYS" != "[]" ] && [ -n "$KEYS" ]; then
    KEY_COUNT=$(echo "$KEYS" | jq '. | length' 2>/dev/null || echo "0")
    if [ "$KEY_COUNT" -gt 0 ]; then
        echo "   ✅ Found $KEY_COUNT key(s)"
        echo "$KEYS" | jq -r '.[]' 2>/dev/null | while read key; do
            echo "      - $key"
        done
    else
        echo "   ⚠️  No keys loaded"
    fi
else
    echo "   ⚠️  No keys loaded"
fi

# 3. Check translator connectivity to Web3Signer
echo ""
echo "3. Translator → Web3Signer Connectivity:"
TRANSLATOR_HEALTH=$(curl -s -m 3 "http://$TRANSLATOR_IP:9545/health" 2>/dev/null || echo "{}")
if echo "$TRANSLATOR_HEALTH" | grep -q "status"; then
    echo "   ✅ Translator is running"
    
    # Check if translator can reach Web3Signer
    if ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@${TRANSLATOR_IP##*.} "curl -s -m 2 $WEB3SIGNER_URL/upcheck 2>/dev/null" 2>/dev/null | grep -q "OK"; then
        echo "   ✅ Translator can reach Web3Signer"
    else
        echo "   ⚠️  Translator connectivity to Web3Signer not verified"
    fi
else
    echo "   ❌ Translator is not responding"
fi

# 4. Check translator configuration
echo ""
echo "4. Translator Configuration:"
WEB3SIGNER_CONFIG=$(ssh -i ~/.ssh/proxmox_translator -o StrictHostKeyChecking=no root@${TRANSLATOR_IP##*.} "grep WEB3SIGNER /opt/rpc-translator-138/.env" 2>/dev/null || echo "")
if [ -n "$WEB3SIGNER_CONFIG" ]; then
    echo "   ✅ Web3Signer configured in translator"
    echo "$WEB3SIGNER_CONFIG" | while read line; do
        if echo "$line" | grep -q "URL"; then
            echo "      $line"
        fi
    done
else
    echo "   ⚠️  Web3Signer configuration not found"
fi

echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "📋 SUMMARY"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "Web3Signer Status:"
if [ "$WEB3SIGNER_HEALTH" = "OK" ]; then
    echo "  ✅ Service is healthy"
else
    echo "  ❌ Service not responding"
fi

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 ""
        echo "Web3Signer is ready for transaction signing!"
    else
        echo "  ⚠️  No keys loaded"
        echo ""
        echo "Next step: Load signing keys into Web3Signer"
        echo "  Run: ./scripts/setup-complete.sh"
    fi
else
    echo "  ⚠️  No keys loaded"
    echo ""
    echo "Next step: Load signing keys into Web3Signer"
    echo "  Run: ./scripts/setup-complete.sh"
fi
