#!/usr/bin/env bash
# Configure all bridge destinations for CCIPWETH9Bridge and CCIPWETH10Bridge on Ethereum Mainnet
# Usage: ./configure-ethereum-mainnet-bridge-destinations.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

# Required variables
ETHEREUM_MAINNET_RPC="${ETHEREUM_MAINNET_RPC:-}"
WETH9_BRIDGE_MAINNET="${CCIPWETH9_BRIDGE_MAINNET:-}"
WETH10_BRIDGE_MAINNET="${CCIPWETH10_BRIDGE_MAINNET:-}"

if [ -z "$ETHEREUM_MAINNET_RPC" ]; then
    log_error "ETHEREUM_MAINNET_RPC not set in .env file"
    exit 1
fi

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

if [ -z "$WETH9_BRIDGE_MAINNET" ] || [ -z "$WETH10_BRIDGE_MAINNET" ]; then
    log_error "Ethereum Mainnet bridge addresses not set in .env file"
    log_error "Please deploy bridges first: bash scripts/deploy-ccipweth9bridge-ethereum-mainnet.sh"
    exit 1
fi

# Destination chain configurations
# Ethereum Mainnet bridges send to: BSC, Polygon, Avalanche, Base, Arbitrum, Optimism, Chain 138
declare -A WETH9_DESTINATIONS=(
    ["11344663589394136015"]="0x8078a09637e47fa5ed34f626046ea2094a5cde5e"  # BSC
    ["4051577828743386545"]="0xa780ef19a041745d353c9432f2a7f5a241335ffe"   # Polygon
    ["6433500567565415381"]="0x8078a09637e47fa5ed34f626046ea2094a5cde5e"  # Avalanche
    ["15971525489660198786"]="0x8078a09637e47fa5ed34f626046ea2094a5cde5e"  # Base
    ["4949039107694359620"]="0x8078a09637e47fa5ed34f626046ea2094a5cde5e"  # Arbitrum
    ["3734403246176062136"]="0x8078a09637e47fa5ed34f626046ea2094a5cde5e"  # Optimism
    ["866240039685049171407962509760789466724431933144813155647626"]="0x89dd12025bfCD38A168455A44B400e913ED33BE2"  # Chain 138
)

declare -A WETH10_DESTINATIONS=(
    ["11344663589394136015"]="0x105f8a15b819948a89153505762444ee9f324684"  # BSC
    ["4051577828743386545"]="0xdab0591e5e89295ffad75a71dcfc30c5625c4fa2"   # Polygon
    ["6433500567565415381"]="0x105f8a15b819948a89153505762444ee9f324684"  # Avalanche
    ["15971525489660198786"]="0x105f8a15b819948a89153505762444ee9f324684"  # Base
    ["4949039107694359620"]="0x105f8a15b819948a89153505762444ee9f324684"  # Arbitrum
    ["3734403246176062136"]="0x105f8a15b819948a89153505762444ee9f324684"  # Optimism
    ["866240039685049171407962509760789466724431933144813155647626"]="0xe0E93247376aa097dB308B92e6Ba36bA015535D0"  # Chain 138
)

declare -A CHAIN_NAMES=(
    ["11344663589394136015"]="BSC"
    ["4051577828743386545"]="Polygon"
    ["6433500567565415381"]="Avalanche"
    ["15971525489660198786"]="Base"
    ["4949039107694359620"]="Arbitrum"
    ["3734403246176062136"]="Optimism"
    ["866240039685049171407962509760789466724431933144813155647626"]="Chain 138"
)

log_info "========================================="
log_info "Configure Ethereum Mainnet Bridge Destinations"
log_info "========================================="
log_info ""
log_info "WETH9 Bridge:  $WETH9_BRIDGE_MAINNET"
log_info "WETH10 Bridge: $WETH10_BRIDGE_MAINNET"
log_info "RPC URL:       ${ETHEREUM_MAINNET_RPC:0:50}..."
log_info ""

# Function to check if destination is already configured
check_destination() {
    local bridge="$1"
    local selector="$2"
    local name="$3"
    
    log_info "Checking $name destination..."
    local result=$(cast call "$bridge" "destinations(uint64)" "$selector" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null || echo "")
    if echo "$result" | grep -qE "(true|enabled)" && ! echo "$result" | grep -q "0x0000000000000000000000000000000000000000$"; then
        return 0  # Already configured
    else
        return 1  # Not configured
    fi
}

# Configure WETH9 Bridge destinations
log_info "Configuring WETH9 Bridge destinations..."
WETH9_COUNT=0
for selector in "${!WETH9_DESTINATIONS[@]}"; do
    chain_name="${CHAIN_NAMES[$selector]}"
    dest_address="${WETH9_DESTINATIONS[$selector]}"
    
    if check_destination "$WETH9_BRIDGE_MAINNET" "$selector" "$chain_name (WETH9)"; then
        log_success "✓ $chain_name already configured for WETH9"
    else
        log_info "Configuring $chain_name for WETH9..."
        if cast send "$WETH9_BRIDGE_MAINNET" \
            "addDestination(uint64,address)" \
            "$selector" \
            "$dest_address" \
            --rpc-url "$ETHEREUM_MAINNET_RPC" \
            --private-key "$PRIVATE_KEY" \
            2>&1 | tee /tmp/weth9-mainnet-config-$chain_name.log | grep -qE "(blockHash|transactionHash|Success)"; then
            log_success "✓ $chain_name configured for WETH9"
            ((WETH9_COUNT++))
        else
            if grep -q "destination already exists" /tmp/weth9-mainnet-config-$chain_name.log 2>/dev/null; then
                log_success "✓ $chain_name already configured for WETH9"
            else
                log_error "✗ Failed to configure $chain_name for WETH9"
                log_info "Check /tmp/weth9-mainnet-config-$chain_name.log for details"
            fi
        fi
    fi
done

log_info ""

# Configure WETH10 Bridge destinations
log_info "Configuring WETH10 Bridge destinations..."
WETH10_COUNT=0
for selector in "${!WETH10_DESTINATIONS[@]}"; do
    chain_name="${CHAIN_NAMES[$selector]}"
    dest_address="${WETH10_DESTINATIONS[$selector]}"
    
    if check_destination "$WETH10_BRIDGE_MAINNET" "$selector" "$chain_name (WETH10)"; then
        log_success "✓ $chain_name already configured for WETH10"
    else
        log_info "Configuring $chain_name for WETH10..."
        if cast send "$WETH10_BRIDGE_MAINNET" \
            "addDestination(uint64,address)" \
            "$selector" \
            "$dest_address" \
            --rpc-url "$ETHEREUM_MAINNET_RPC" \
            --private-key "$PRIVATE_KEY" \
            2>&1 | tee /tmp/weth10-mainnet-config-$chain_name.log | grep -qE "(blockHash|transactionHash|Success)"; then
            log_success "✓ $chain_name configured for WETH10"
            ((WETH10_COUNT++))
        else
            if grep -q "destination already exists" /tmp/weth10-mainnet-config-$chain_name.log 2>/dev/null; then
                log_success "✓ $chain_name already configured for WETH10"
            else
                log_error "✗ Failed to configure $chain_name for WETH10"
                log_info "Check /tmp/weth10-mainnet-config-$chain_name.log for details"
            fi
        fi
    fi
done

log_info ""
log_success "========================================="
log_success "Bridge Configuration Complete!"
log_success "========================================="
log_info ""
log_info "Summary:"
log_info "  WETH9 destinations configured:  $WETH9_COUNT new"
log_info "  WETH10 destinations configured: $WETH10_COUNT new"
log_info ""
log_info "All 7 destination chains configured for both bridges"
log_info ""

