#!/usr/bin/env bash
# Force configure Ethereum Mainnet by using extremely high gas price
# Usage: ./force-configure-ethereum-mainnet.sh

set -uo 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="0x8078a09637e47fa5ed34f626046ea2094a5cde5e"
WETH10_MAINNET_BRIDGE="0x105f8a15b819948a89153505762444ee9f324684"

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

log_info "========================================="
log_info "Force Configure Ethereum Mainnet"
log_info "========================================="
log_info ""

# Use extremely high gas price to force replacement
EXTREME_GAS="10000000000000"  # 10,000 gwei

# Configure WETH9
log_info "Configuring WETH9 bridge (using extreme gas price)..."
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 "$EXTREME_GAS" \
    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" | head -1 || echo "Unknown")
    log_error "✗ WETH9 configuration failed: $ERR"
fi

log_info ""

# Configure WETH10
log_info "Configuring WETH10 bridge (using extreme gas price)..."
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 "$EXTREME_GAS" \
    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" | head -1 || echo "Unknown")
    log_error "✗ WETH10 configuration failed: $ERR"
fi

log_info ""
log_success "========================================="
log_success "Configuration Attempt Complete"
log_success "========================================="
log_info ""

# Verify configuration
log_info "Verifying configuration..."
sleep 5

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 -qE "^0x0+$" && ! echo "$WETH9_CHECK" | grep -qE "^0x0000000000000000000000000000000000000000$"; then
    log_success "✓ WETH9 bridge is now configured"
else
    log_warn "⚠ WETH9 bridge may still need configuration"
fi

if [ -n "$WETH10_CHECK" ] && ! echo "$WETH10_CHECK" | grep -qE "^0x0+$" && ! echo "$WETH10_CHECK" | grep -qE "^0x0000000000000000000000000000000000000000$"; then
    log_success "✓ WETH10 bridge is now configured"
else
    log_warn "⚠ WETH10 bridge may still need configuration"
fi

