#!/usr/bin/env bash
# Verify contract deployments on-chain

set -e

cd "$(dirname "$0")/../.."

# Color codes

echo "=== On-Chain Deployment Verification ==="

# Load environment variables
if [ -f .env ]; then
    source .env 2>/dev/null || true
fi

if ! command -v cast &> /dev/null; then
    log_error "Error: cast (Foundry) not found. Install Foundry to verify on-chain deployments."
    exit 1
fi

# Check Mainnet contracts
if [ -n "$ETHEREUM_MAINNET_RPC" ]; then
    log_info "Ethereum Mainnet:"
    
    # WETH9
    echo -n "  WETH9 (0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2): "
    WETH9_CODE=$(cast code 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -n "$WETH9_CODE" ] && [ "$WETH9_CODE" != "0x" ] && [ ${#WETH9_CODE} -gt 2 ]; then
        log_success "✅ Deployed"
    else
        log_error "❌ Not found"
    fi
    
    # WETH10
    echo -n "  WETH10 (0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f): "
    WETH10_CODE=$(cast code 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if [ -n "$WETH10_CODE" ] && [ "$WETH10_CODE" != "0x" ] && [ ${#WETH10_CODE} -gt 2 ]; then
        log_success "✅ Deployed"
    else
        log_error "❌ Not found"
    fi
    
    # CCIPLogger
    if [ -n "$MAINNET_CCIP_LOGGER" ] && [ "$MAINNET_CCIP_LOGGER" != "" ]; then
        echo -n "  CCIPLogger ($MAINNET_CCIP_LOGGER): "
        LOGGER_CODE=$(cast code "$MAINNET_CCIP_LOGGER" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
        if [ -n "$LOGGER_CODE" ] && [ "$LOGGER_CODE" != "0x" ] && [ ${#LOGGER_CODE} -gt 2 ]; then
            log_success "✅ Deployed"
        else
            log_error "❌ Not found"
        fi
    else
        echo -e "  CCIPLogger: ${RED}❌ Not configured in .env${NC}"
    fi
    
    # CCIPWETH9Bridge
    if [ -n "$MAINNET_CCIP_WETH9_BRIDGE" ] && [ "$MAINNET_CCIP_WETH9_BRIDGE" != "" ]; then
        echo -n "  CCIPWETH9Bridge ($MAINNET_CCIP_WETH9_BRIDGE): "
        BRIDGE9_CODE=$(cast code "$MAINNET_CCIP_WETH9_BRIDGE" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
        if [ -n "$BRIDGE9_CODE" ] && [ "$BRIDGE9_CODE" != "0x" ] && [ ${#BRIDGE9_CODE} -gt 2 ]; then
            log_success "✅ Deployed"
        else
            log_error "❌ Not found"
        fi
    else
        echo -e "  CCIPWETH9Bridge: ${RED}❌ Not configured in .env${NC}"
    fi
    
    # CCIPWETH10Bridge
    if [ -n "$MAINNET_CCIP_WETH10_BRIDGE" ] && [ "$MAINNET_CCIP_WETH10_BRIDGE" != "" ]; then
        echo -n "  CCIPWETH10Bridge ($MAINNET_CCIP_WETH10_BRIDGE): "
        BRIDGE10_CODE=$(cast code "$MAINNET_CCIP_WETH10_BRIDGE" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
        if [ -n "$BRIDGE10_CODE" ] && [ "$BRIDGE10_CODE" != "0x" ] && [ ${#BRIDGE10_CODE} -gt 2 ]; then
            log_success "✅ Deployed"
        else
            log_error "❌ Not found"
        fi
    else
        echo -e "  CCIPWETH10Bridge: ${RED}❌ Not configured in .env${NC}"
    fi
else
    log_warn "⚠️  Mainnet RPC not configured"
fi

log_info "Chain-138:"

if [ -n "$CHAIN138_RPC_URL" ]; then
    # CCIPTxReporter
    if [ -n "$CHAIN138_CCIP_REPORTER" ] && [ "$CHAIN138_CCIP_REPORTER" != "" ]; then
        echo -n "  CCIPTxReporter ($CHAIN138_CCIP_REPORTER): "
        REPORTER_CODE=$(cast code "$CHAIN138_CCIP_REPORTER" --rpc-url "$CHAIN138_RPC_URL" 2>/dev/null || echo "")
        if [ -n "$REPORTER_CODE" ] && [ "$REPORTER_CODE" != "0x" ] && [ ${#REPORTER_CODE} -gt 2 ]; then
            log_success "✅ Deployed"
        else
            log_warn "⚠️  Cannot verify (RPC may not be accessible)"
        fi
    else
        echo -e "  CCIPTxReporter: ${RED}❌ Not configured in .env${NC}"
    fi
else
    log_warn "⚠️  Chain-138 RPC not configured"
fi

echo "=== Verification Complete ==="
