#!/usr/bin/env bash
# Verify bridge configuration and functionality
# Usage: ./verify-bridge-configuration.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# Load environment variables
if [ -f "$SOURCE_PROJECT/.env" ]; then
    source "$SOURCE_PROJECT/.env"
else
    log_error ".env file not found in $SOURCE_PROJECT"
    exit 1
fi

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
WETH10_BRIDGE="${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}"
WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10_ADDRESS="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"

# Destination chain configurations
declare -A CHAIN_SELECTORS=(
    ["BSC"]="11344663589394136015"
    ["Polygon"]="4051577828743386545"
    ["Avalanche"]="6433500567565415381"
    ["Base"]="15971525489660198786"
    ["Arbitrum"]="4949039107694359620"
    ["Optimism"]="3734403246176062136"
)

log_info "========================================="
log_info "Verify Bridge Configuration"
log_info "========================================="
log_info ""
log_info "WETH9 Bridge:  $WETH9_BRIDGE"
log_info "WETH10 Bridge: $WETH10_BRIDGE"
log_info "RPC URL:       $RPC_URL"
log_info ""

# Get deployer address
if [ -z "${PRIVATE_KEY:-}" ]; then
    log_error "PRIVATE_KEY not set in .env file"
    exit 1
fi

DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
if [ -z "$DEPLOYER" ]; then
    log_error "Failed to get deployer address"
    exit 1
fi

log_info "Deployer: $DEPLOYER"
log_info ""

# Verify WETH9 Bridge destinations
log_info "Verifying WETH9 Bridge destinations..."
WETH9_CONFIGURED=0
for chain in "${!CHAIN_SELECTORS[@]}"; do
    selector="${CHAIN_SELECTORS[$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
        log_success "✓ $chain configured"
        ((WETH9_CONFIGURED++))
    else
        log_error "✗ $chain not configured"
    fi
done

log_info ""

# Verify WETH10 Bridge destinations
log_info "Verifying WETH10 Bridge destinations..."
WETH10_CONFIGURED=0
for chain in "${!CHAIN_SELECTORS[@]}"; do
    selector="${CHAIN_SELECTORS[$chain]}"
    result=$(cast call "$WETH10_BRIDGE" "destinations(uint64)" "$selector" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    
    if [ -n "$result" ] && ! echo "$result" | grep -q "0x0000000000000000000000000000000000000000$"; then
        log_success "✓ $chain configured"
        ((WETH10_CONFIGURED++))
    else
        log_error "✗ $chain not configured"
    fi
done

log_info ""

# Test fee calculation
log_info "Testing fee calculation..."
TEST_AMOUNT="10000000000000000"  # 0.01 ETH
BSC_SELECTOR="${CHAIN_SELECTORS[BSC]}"

WETH9_FEE=$(cast call "$WETH9_BRIDGE" "calculateFee(uint64,uint256)" "$BSC_SELECTOR" "$TEST_AMOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
WETH10_FEE=$(cast call "$WETH10_BRIDGE" "calculateFee(uint64,uint256)" "$BSC_SELECTOR" "$TEST_AMOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

if [ "$WETH9_FEE" != "0" ] && [ -n "$WETH9_FEE" ]; then
    log_success "✓ WETH9 fee calculation working: $WETH9_FEE wei"
else
    log_warn "⚠ WETH9 fee calculation returned 0 or error"
fi

if [ "$WETH10_FEE" != "0" ] && [ -n "$WETH10_FEE" ]; then
    log_success "✓ WETH10 fee calculation working: $WETH10_FEE wei"
else
    log_warn "⚠ WETH10 fee calculation returned 0 or error"
fi

log_info ""

# Check token balances
log_info "Checking token balances..."
WETH9_BALANCE=$(cast call "$WETH9_ADDRESS" "balanceOf(address)" "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
WETH10_BALANCE=$(cast call "$WETH10_ADDRESS" "balanceOf(address)" "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

if [ "$WETH9_BALANCE" != "0" ]; then
    WETH9_ETH=$(echo "scale=6; $WETH9_BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0")
    log_info "WETH9 Balance: $WETH9_ETH ETH"
else
    log_info "WETH9 Balance: 0 ETH"
fi

if [ "$WETH10_BALANCE" != "0" ]; then
    WETH10_ETH=$(echo "scale=6; $WETH10_BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0")
    log_info "WETH10 Balance: $WETH10_ETH ETH"
else
    log_info "WETH10 Balance: 0 ETH"
fi

log_info ""

# Summary
log_success "========================================="
log_success "Verification Complete!"
log_success "========================================="
log_info ""
log_info "Summary:"
log_info "  WETH9 destinations configured:  $WETH9_CONFIGURED/6"
log_info "  WETH10 destinations configured: $WETH10_CONFIGURED/6"
log_info "  Fee calculation: ✅ Working"
log_info ""

if [ "$WETH9_CONFIGURED" -eq 6 ] && [ "$WETH10_CONFIGURED" -eq 6 ]; then
    log_success "✅ All bridges fully configured and ready!"
else
    log_warn "⚠ Some destinations may not be configured"
fi

