#!/usr/bin/env bash
# Configure Chain 138 ↔ Ethereum mainnet CCIP WETH bridge destinations.
# Uses CCIPWETH9Bridge / CCIPWETH10Bridge addDestination(uint64,address) (receiver = peer bridge on the other chain).
# For Gnosis/Cronos/Celo/Wemix, use complete-config-ready-chains.sh instead.
#
# Env: PRIVATE_KEY; bridge addresses (see below); RPC_URL_138; ETHEREUM_MAINNET_RPC or RPC_URL_MAINNET.
#
# shellcheck disable=SC1090

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "$PROJECT_ROOT"
fi

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

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

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

MAINNET_CHAIN_SELECTOR="${MAINNET_CHAIN_SELECTOR:-5009297550715157269}"
CHAIN138_CHAIN_SELECTOR="${CHAIN138_CHAIN_SELECTOR:-${CHAIN138_SELECTOR:-}}"

# 138-side bridges (aliases match .env CCIP* names)
WETH9_BRIDGE_138="${WETH9_BRIDGE_138:-${CCIPWETH9_BRIDGE_CHAIN138:-}}"
WETH10_BRIDGE_138="${WETH10_BRIDGE_138:-${CCIPWETH10_BRIDGE_CHAIN138:-}}"
# Mainnet peer *bridge* contracts (not WETH token addresses)
WETH9_MAINNET="${WETH9_MAINNET:-${MAINNET_CCIP_WETH9_BRIDGE:-}}"
WETH10_MAINNET="${WETH10_MAINNET:-${MAINNET_CCIP_WETH10_BRIDGE:-}}"

echo -e "${GREEN}=== Bridge destination configuration (138 ↔ Mainnet) ===${NC}\n"

if [[ -z "$PRIVATE_KEY" ]]; then
  echo -e "${RED}ERROR: PRIVATE_KEY not set (env or .env)${NC}" >&2
  exit 1
fi

if [[ -z "$WETH9_BRIDGE_138" || -z "$WETH10_BRIDGE_138" ]]; then
  echo -e "${RED}ERROR: Set WETH9_BRIDGE_138 / WETH10_BRIDGE_138 or CCIPWETH9_BRIDGE_CHAIN138 / CCIPWETH10_BRIDGE_CHAIN138${NC}" >&2
  exit 1
fi

if [[ -z "$WETH9_MAINNET" || -z "$WETH10_MAINNET" ]]; then
  echo -e "${RED}ERROR: Set mainnet peer bridge addresses: WETH9_MAINNET / WETH10_MAINNET${NC}" >&2
  echo -e "  (or MAINNET_CCIP_WETH9_BRIDGE / MAINNET_CCIP_WETH10_BRIDGE). These must be the CCIP bridge contracts on Ethereum, not WETH token addresses.${NC}" >&2
  exit 1
fi

add_destination() {
  local bridge_address=$1
  local chain_selector=$2
  local receiver_bridge=$3
  local rpc_url=$4
  local network_name=$5

  echo -e "${GREEN}Adding destination on $network_name bridge...${NC}"
  cast send "${bridge_address,,}" \
    "addDestination(uint64,address)" \
    "$chain_selector" \
    "${receiver_bridge,,}" \
    --rpc-url "$rpc_url" \
    --private-key "$PRIVATE_KEY" \
    --legacy \
    --gas-limit 250000 \
    --gas-price 2000000000

  echo -e "${GREEN}✓ Destination added${NC}\n"
}

verify_destinations() {
  local bridge_address=$1
  local rpc_url=$2
  local network_name=$3

  echo -e "${GREEN}Verifying destinations on $network_name...${NC}"
  local chains
  if chains=$(cast call "${bridge_address,,}" \
    "getDestinationChains()(uint64[])" \
    --rpc-url "$rpc_url" 2>/dev/null); then
    echo -e "${GREEN}Destination chains: $chains${NC}\n"
  else
    echo -e "${YELLOW}Could not read getDestinationChains (cast/RPC). Try cast rpc eth_call or another RPC.${NC}\n"
  fi
}

echo -e "${YELLOW}=== Chain 138 → Mainnet ===${NC}\n"

echo -e "${GREEN}1. WETH9 bridge on 138 → Mainnet peer${NC}"
add_destination "$WETH9_BRIDGE_138" "$MAINNET_CHAIN_SELECTOR" "$WETH9_MAINNET" "$RPC_URL_138" "Chain 138 WETH9"

echo -e "${GREEN}2. WETH10 bridge on 138 → Mainnet peer${NC}"
add_destination "$WETH10_BRIDGE_138" "$MAINNET_CHAIN_SELECTOR" "$WETH10_MAINNET" "$RPC_URL_138" "Chain 138 WETH10"

echo -e "${YELLOW}=== Verify 138 bridges ===${NC}\n"
verify_destinations "$WETH9_BRIDGE_138" "$RPC_URL_138" "WETH9 Bridge"
verify_destinations "$WETH10_BRIDGE_138" "$RPC_URL_138" "WETH10 Bridge"

if [[ -z "$CHAIN138_CHAIN_SELECTOR" ]]; then
  echo -e "${YELLOW}NOTE: CHAIN138_CHAIN_SELECTOR / CHAIN138_SELECTOR not set.${NC}"
  echo -e "${YELLOW}To add Chain 138 as a destination on Mainnet bridges, set the selector then run addDestination on mainnet (see complete-config-ready-chains.sh pattern).${NC}"
  echo -e "${YELLOW}Router getChainSelector() may revert on custom routers; see docs/deployment/CHAIN138_SELECTOR_NOTES.md${NC}\n"
else
  echo -e "${YELLOW}=== Mainnet → Chain 138 ===${NC}"
  echo -e "${YELLOW}CHAIN138_CHAIN_SELECTOR=$CHAIN138_CHAIN_SELECTOR — run addDestination on each Mainnet bridge with this selector and the 138 peer bridge address (not automated here).${NC}\n"
fi

echo -e "${GREEN}=== Done ===${NC}\n"
echo -e "Next: fund bridges with LINK; test transfers; monitor fees."
