#!/usr/bin/env bash
# Add Ethereum Mainnet to bridge destinations
# Usage: ./add-ethereum-mainnet-bridge.sh [weth9_bridge_address] [weth10_bridge_address]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && 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"; }

source "$SOURCE_PROJECT/.env"

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
WETH10_BRIDGE="${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}"

ETHEREUM_MAINNET_SELECTOR="5009297550715157269"
WETH9_MAINNET_BRIDGE="${1:-}"
WETH10_MAINNET_BRIDGE="${2:-}"

if [ -z "$WETH9_MAINNET_BRIDGE" ] || [ -z "$WETH10_MAINNET_BRIDGE" ]; then
    log_error "Usage: $0 <weth9_mainnet_bridge_address> <weth10_mainnet_bridge_address>"
    log_info "Example: $0 0x... 0x..."
    exit 1
fi

DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null)

log_info "========================================="
log_info "Add Ethereum Mainnet to Bridges"
log_info "========================================="
log_info ""
log_info "Ethereum Mainnet Selector: $ETHEREUM_MAINNET_SELECTOR"
log_info "WETH9 Mainnet Bridge: $WETH9_MAINNET_BRIDGE"
log_info "WETH10 Mainnet Bridge: $WETH10_MAINNET_BRIDGE"
log_info ""

# Check if already configured
log_info "Checking current configuration..."

WETH9_CHECK=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$ETHEREUM_MAINNET_SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
WETH10_CHECK=$(cast call "$WETH10_BRIDGE" "destinations(uint64)" "$ETHEREUM_MAINNET_SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")

if [ -n "$WETH9_CHECK" ] && ! echo "$WETH9_CHECK" | grep -q "0x0000000000000000000000000000000000000000$"; then
    log_success "✓ WETH9 bridge already configured for Ethereum Mainnet"
else
    log_info "Configuring WETH9 bridge for Ethereum Mainnet..."
    CURRENT_NONCE=$(cast nonce "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    
    TX_OUTPUT=$(cast send "$WETH9_BRIDGE" \
        "addDestination(uint64,address)" \
        "$ETHEREUM_MAINNET_SELECTOR" \
        "$WETH9_MAINNET_BRIDGE" \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --gas-price 20000000000 \
        --nonce "$CURRENT_NONCE" \
        2>&1 || echo "FAILED")
    
    if echo "$TX_OUTPUT" | grep -qE "transactionHash|Success"; then
        HASH=$(echo "$TX_OUTPUT" | grep -oE "transactionHash[[:space:]]+0x[0-9a-fA-F]{64}" | awk '{print $2}' || echo "")
        log_success "✓ WETH9 bridge configured: $HASH"
        sleep 10
    else
        ERR=$(echo "$TX_OUTPUT" | grep -E "Error|reverted|already exists" | head -1 || echo "Unknown")
        log_error "✗ WETH9 configuration failed: $ERR"
    fi
fi

if [ -n "$WETH10_CHECK" ] && ! echo "$WETH10_CHECK" | grep -q "0x0000000000000000000000000000000000000000$"; then
    log_success "✓ WETH10 bridge already configured for Ethereum Mainnet"
else
    log_info "Configuring WETH10 bridge for Ethereum Mainnet..."
    CURRENT_NONCE=$(cast nonce "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    
    TX_OUTPUT=$(cast send "$WETH10_BRIDGE" \
        "addDestination(uint64,address)" \
        "$ETHEREUM_MAINNET_SELECTOR" \
        "$WETH10_MAINNET_BRIDGE" \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --gas-price 20000000000 \
        --nonce "$CURRENT_NONCE" \
        2>&1 || echo "FAILED")
    
    if echo "$TX_OUTPUT" | grep -qE "transactionHash|Success"; then
        HASH=$(echo "$TX_OUTPUT" | grep -oE "transactionHash[[:space:]]+0x[0-9a-fA-F]{64}" | awk '{print $2}' || echo "")
        log_success "✓ WETH10 bridge configured: $HASH"
        sleep 10
    else
        ERR=$(echo "$TX_OUTPUT" | grep -E "Error|reverted|already exists" | head -1 || echo "Unknown")
        log_error "✗ WETH10 configuration failed: $ERR"
    fi
fi

log_info ""
log_success "========================================="
log_success "Ethereum Mainnet Configuration Complete!"
log_success "========================================="

