#!/usr/bin/env bash
# Execute bridge destination configuration

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"


log_info "=== Executing Bridge Configuration ==="

# Load environment
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
fi

MAINNET_RPC="${ETHEREUM_MAINNET_RPC:-https://eth.llamarpc.com}"
CHAIN138_RPC="${RPC_URL:-https://rpc.d-bis.org}"
PRIVATE_KEY="${PRIVATE_KEY}"
if [[ ! "$PRIVATE_KEY" =~ ^0x ]]; then
    PRIVATE_KEY="0x$PRIVATE_KEY"
fi

WETH9_MAINNET=$(grep "CCIPWETH9_BRIDGE_MAINNET=" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")
WETH10_MAINNET=$(grep "CCIPWETH10_BRIDGE_MAINNET=" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")
WETH9_CHAIN138=$(grep "CCIPWETH9_BRIDGE_CHAIN138=" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")
WETH10_CHAIN138=$(grep "CCIPWETH10_BRIDGE_CHAIN138=" "$PROJECT_ROOT/.env" 2>/dev/null | cut -d'=' -f2 | tr -d ' "' || echo "")

CHAIN138_SELECTOR="${CHAIN138_SELECTOR:-0x000000000000008a}"
ETH_SELECTOR="${ETH_MAINNET_SELECTOR:-0x500147}"

if [ -z "$WETH9_MAINNET" ] || [ -z "$WETH9_CHAIN138" ] || [ -z "$WETH10_MAINNET" ] || [ -z "$WETH10_CHAIN138" ]; then
    log_error "Error: Bridge addresses not found in .env"
    exit 1
fi

echo "Configuring WETH9 Bridge..."
echo "  Adding Chain-138 destination to Mainnet bridge..."
if cast send "$WETH9_MAINNET" "addDestination(uint64,address)" "$CHAIN138_SELECTOR" "$WETH9_CHAIN138" --rpc-url "$MAINNET_RPC" --private-key "$PRIVATE_KEY" --legacy 2>&1 | grep -q "Success\|success"; then
    log_success "✅ WETH9 Mainnet → Chain-138 configured"
else
    log_warn "⚠️  Configuration may have failed - check transaction"
fi

echo "  Adding Mainnet destination to Chain-138 bridge..."
if cast send "$WETH9_CHAIN138" "addDestination(uint64,address)" "$ETH_SELECTOR" "$WETH9_MAINNET" --rpc-url "$CHAIN138_RPC" --private-key "$PRIVATE_KEY" --legacy 2>&1 | grep -q "Success\|success"; then
    log_success "✅ WETH9 Chain-138 → Mainnet configured"
else
    log_warn "⚠️  Configuration may have failed - check transaction"
fi

echo "Configuring WETH10 Bridge..."
echo "  Adding Chain-138 destination to Mainnet bridge..."
if cast send "$WETH10_MAINNET" "addDestination(uint64,address)" "$CHAIN138_SELECTOR" "$WETH10_CHAIN138" --rpc-url "$MAINNET_RPC" --private-key "$PRIVATE_KEY" --legacy 2>&1 | grep -q "Success\|success"; then
    log_success "✅ WETH10 Mainnet → Chain-138 configured"
else
    log_warn "⚠️  Configuration may have failed - check transaction"
fi

echo "  Adding Mainnet destination to Chain-138 bridge..."
if cast send "$WETH10_CHAIN138" "addDestination(uint64,address)" "$ETH_SELECTOR" "$WETH10_MAINNET" --rpc-url "$CHAIN138_RPC" --private-key "$PRIVATE_KEY" --legacy 2>&1 | grep -q "Success\|success"; then
    log_success "✅ WETH10 Chain-138 → Mainnet configured"
else
    log_warn "⚠️  Configuration may have failed - check transaction"
fi

log_success "✅ Bridge configuration complete!"
