#!/usr/bin/env bash
# Test service-to-contract integration
# Usage: ./test-service-integration.sh

RPC_URL="${RPC_URL:-http://192.168.11.250:8545}"

echo "========================================="
echo "Service-to-Contract Integration Test"
echo "RPC: $RPC_URL"
echo "========================================="
echo ""

# Test CCIP Monitor integration
echo "1. Testing CCIP Monitor Integration..."
CCIP_MONITOR_VMID=3501
if command -v pct &>/dev/null; then
    CONTAINER_STATUS=$(pct status $CCIP_MONITOR_VMID 2>/dev/null || echo "not found")
    if [[ "$CONTAINER_STATUS" == *"running"* ]]; then
        SERVICE_STATUS=$(pct exec $CCIP_MONITOR_VMID -- systemctl is-active ccip-monitor.service 2>/dev/null || echo "inactive")
        if [[ "$SERVICE_STATUS" == "active" ]]; then
            echo "   ✅ CCIP Monitor service is running (VMID $CCIP_MONITOR_VMID)"
            
            # Check if service can connect to RPC
            RPC_CHECK=$(pct exec $CCIP_MONITOR_VMID -- curl -s -X POST "$RPC_URL" \
                -H "Content-Type: application/json" \
                -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' 2>/dev/null || echo "failed")
            if echo "$RPC_CHECK" | grep -q '"result"'; then
                echo "   ✅ CCIP Monitor can connect to RPC"
            else
                echo "   ⚠️  CCIP Monitor RPC connection test failed"
            fi
        else
            echo "   ⚠️  CCIP Monitor service is $SERVICE_STATUS"
        fi
    else
        echo "   ⚠️  CCIP Monitor container status: $CONTAINER_STATUS"
    fi
else
    echo "   ⚠️  pct command not available (not on Proxmox host)"
    echo "   💡 CCIP Monitor should be on VMID $CCIP_MONITOR_VMID"
fi

# Test Oracle Publisher integration
echo ""
echo "2. Testing Oracle Publisher Integration..."
ORACLE_PUBLISHER_VMID=3500
if command -v pct &>/dev/null; then
    CONTAINER_STATUS=$(pct status $ORACLE_PUBLISHER_VMID 2>/dev/null || echo "not found")
    if [[ "$CONTAINER_STATUS" == *"running"* ]]; then
        echo "   ✅ Oracle Publisher container is running (VMID $ORACLE_PUBLISHER_VMID)"
        SERVICE_STATUS=$(pct exec $ORACLE_PUBLISHER_VMID -- systemctl is-active oracle-publisher.service 2>/dev/null || echo "inactive")
        if [[ "$SERVICE_STATUS" == "active" ]]; then
            echo "   ✅ Oracle Publisher service is active"
        else
            echo "   ⚠️  Oracle Publisher service is $SERVICE_STATUS"
        fi
    else
        echo "   ⚠️  Oracle Publisher container status: $CONTAINER_STATUS"
    fi
else
    echo "   ⚠️  pct command not available (not on Proxmox host)"
    echo "   💡 Oracle Publisher should be on VMID $ORACLE_PUBLISHER_VMID"
fi

# Test contract accessibility from services
echo ""
echo "3. Testing Contract Accessibility..."

ORACLE_PROXY="0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6"
CCIP_ROUTER="0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e"

# Test Oracle Proxy
ORACLE_BYTECODE=$(cast code "$ORACLE_PROXY" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$ORACLE_BYTECODE" ] && [ "$ORACLE_BYTECODE" != "0x" ]; then
    echo "   ✅ Oracle Proxy accessible: $ORACLE_PROXY"
else
    echo "   ❌ Oracle Proxy not accessible: $ORACLE_PROXY"
fi

# Test CCIP Router
CCIP_BYTECODE=$(cast code "$CCIP_ROUTER" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$CCIP_BYTECODE" ] && [ "$CCIP_BYTECODE" != "0x" ]; then
    echo "   ✅ CCIP Router accessible: $CCIP_ROUTER"
else
    echo "   ❌ CCIP Router not accessible: $CCIP_ROUTER"
fi

echo ""
echo "========================================="
echo "Integration Test Summary"
echo "========================================="
echo "✅ All core contracts are accessible via RPC"
echo "💡 Service integration depends on container/service status"
echo ""
