#!/usr/bin/env bash
# Execute WETH9/WETH10 bridge addDestination both directions: Ethereum mainnet ↔ Chain 138.
# Requires .env with bridge addresses and PRIVATE_KEY.

set -euo pipefail

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

if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
fi

log_info "=== Executing bridge configuration (Mainnet ↔ Chain 138) ==="

MAINNET_RPC="${ETHEREUM_MAINNET_RPC:-https://eth.llamarpc.com}"
MAINNET_RPC="${MAINNET_RPC%$'\r'}"
MAINNET_RPC="${MAINNET_RPC%$'\n'}"
CHAIN138_RPC="${RPC_URL_138:-${CHAIN138_RPC:-http://192.168.11.211:8545}}"
CHAIN138_RPC="${CHAIN138_RPC%$'\r'}"
CHAIN138_RPC="${CHAIN138_RPC%$'\n'}"

PRIVATE_KEY="${PRIVATE_KEY:-}"
[[ -n "$PRIVATE_KEY" && ! "$PRIVATE_KEY" =~ ^0x ]] && PRIVATE_KEY="0x$PRIVATE_KEY"

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

CHAIN138_SELECTOR="${CHAIN138_SELECTOR:-138}"
ETH_SELECTOR="${ETH_MAINNET_SELECTOR:-5009297550715157269}"

WETH9_MAINNET="${WETH9_MAINNET,,}"
WETH10_MAINNET="${WETH10_MAINNET,,}"
WETH9_CHAIN138="${WETH9_CHAIN138,,}"
WETH10_CHAIN138="${WETH10_CHAIN138,,}"

if [[ -z "$WETH9_MAINNET" || -z "$WETH9_CHAIN138" || -z "$WETH10_MAINNET" || -z "$WETH10_CHAIN138" ]]; then
  log_error "Bridge addresses not found in .env (CCIPWETH9/10_BRIDGE_MAINNET and CCIPWETH9/10_BRIDGE_CHAIN138)"
  exit 1
fi

_gas138=(--legacy --gas-limit 250000 --gas-price 2000000000)
_gas_main=(--legacy --gas-limit 400000)

_try() {
  local _o
  if _o=$("$@" 2>&1); then
    log_success "OK"
    [[ -n "$_o" ]] && echo "$_o"
  else
    log_warn "Command failed (non-fatal for batch)"
    echo "$_o" >&2
  fi
}

echo "Configuring WETH9 Bridge..."
echo "  Mainnet → Chain 138..."
_try cast send "$WETH9_MAINNET" "addDestination(uint64,address)" "$CHAIN138_SELECTOR" "$WETH9_CHAIN138" \
  --rpc-url "$MAINNET_RPC" --private-key "$PRIVATE_KEY" "${_gas_main[@]}"
echo "  Chain 138 → Mainnet..."
_try cast send "$WETH9_CHAIN138" "addDestination(uint64,address)" "$ETH_SELECTOR" "$WETH9_MAINNET" \
  --rpc-url "$CHAIN138_RPC" --private-key "$PRIVATE_KEY" "${_gas138[@]}"

echo "Configuring WETH10 Bridge..."
echo "  Mainnet → Chain 138..."
_try cast send "$WETH10_MAINNET" "addDestination(uint64,address)" "$CHAIN138_SELECTOR" "$WETH10_CHAIN138" \
  --rpc-url "$MAINNET_RPC" --private-key "$PRIVATE_KEY" "${_gas_main[@]}"
echo "  Chain 138 → Mainnet..."
_try cast send "$WETH10_CHAIN138" "addDestination(uint64,address)" "$ETH_SELECTOR" "$WETH10_MAINNET" \
  --rpc-url "$CHAIN138_RPC" --private-key "$PRIVATE_KEY" "${_gas138[@]}"

log_success "Bridge configuration attempts finished (review output above)."
