#!/usr/bin/env bash
# Comprehensive health check for bridge system

source /home/intlc/projects/smom-dbis-138/.env
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"

echo "=== Bridge System Health Check ==="
echo ""

# Check RPC connectivity
echo "1. RPC Connectivity:"
if cast block-number --rpc-url "$RPC_URL" >/dev/null 2>&1; then
    echo "  ✅ RPC is accessible"
else
    echo "  ❌ RPC is not accessible"
fi

# Check bridge contracts
echo ""
echo "2. Bridge Contracts:"
WETH9_BRIDGE="0x89dd12025bfCD38A168455A44B400e913ED33BE2"
WETH10_BRIDGE="0xe0E93247376aa097dB308B92e6Ba36bA015535D0"

if cast code "$WETH9_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
    echo "  ✅ WETH9 Bridge deployed"
else
    echo "  ❌ WETH9 Bridge not found"
fi

if cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
    echo "  ✅ WETH10 Bridge deployed"
else
    echo "  ❌ WETH10 Bridge not found"
fi

# Check destination chains
echo ""
echo "3. Destination Chains:"
declare -A CHAINS=(
    ["BSC"]="11344663589394136015"
    ["Polygon"]="4051577828743386545"
    ["Avalanche"]="6433500567565415381"
    ["Base"]="15971525489660198786"
    ["Arbitrum"]="4949039107694359620"
    ["Optimism"]="3734403246176062136"
    ["Ethereum"]="5009297550715157269"
)

for chain in "${!CHAINS[@]}"; do
    selector="${CHAINS[$chain]}"
    result=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$selector" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$result" ] && ! echo "$result" | grep -q "0x0000000000000000000000000000000000000000$"; then
        echo "  ✅ $chain configured"
    else
        echo "  ❌ $chain not configured"
    fi
done
