#!/usr/bin/env bash
# Wire Monad↔138 relay lanes after CCIP relay contracts are deployed.
#
# 138→Monad: updateDestination on 138 bridges → CCIP_RELAY_BRIDGE_MONAD
# Monad→138: addDestination on CCIPWETH9_BRIDGE_MONAD_SEND → CCIP_RELAY_BRIDGE_CHAIN138
# Authorize 138 WETH relay bridge on CCIP_RELAY_ROUTER_CHAIN138
#
# Usage:
#   DRY_RUN=1 ./scripts/deployment/wire-monad-chain138-relay.sh
#   ./scripts/deployment/wire-monad-chain138-relay.sh --execute

set -euo pipefail

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

# shellcheck disable=SC1090
source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
load_deployment_env --repo-root "$PROJECT_ROOT"

EXECUTE=0
DRY_RUN="${DRY_RUN:-0}"
while [[ $# -gt 0 ]]; do
  case "$1" in
    --execute) EXECUTE=1; shift ;;
    --dry-run) DRY_RUN=1; shift ;;
    *) echo "Unknown arg: $1" >&2; exit 1 ;;
  esac
done

RPC138="${RPC_URL_138:-http://192.168.11.211:8545}"
MONAD_RPC="${MONAD_MAINNET_RPC_PUBLIC:-https://rpc.monad.xyz}"
if [[ "${MONAD_MAINNET_RPC:-}" =~ infura\.io/v3 ]] && [[ "${MONAD_MAINNET_RPC_FORCE_INFURA:-0}" != "1" ]]; then
  MONAD_RPC="${MONAD_MAINNET_RPC_PUBLIC:-https://rpc.monad.xyz}"
fi

MONAD_SEL="${MONAD_SELECTOR:-8481857512324358265}"
CH138_SEL="${CHAIN138_SELECTOR:-}"
W9_138="${CCIPWETH9_BRIDGE_CHAIN138:-}"
W10_138="${CCIPWETH10_BRIDGE_CHAIN138:-}"
MONAD_RELAY_BRIDGE="${CCIP_RELAY_BRIDGE_MONAD:-}"
MONAD_SEND_BRIDGE="${CCIPWETH9_BRIDGE_MONAD_SEND:-}"
CH138_RELAY_ROUTER="${CCIP_RELAY_ROUTER_CHAIN138:-}"
CH138_RELAY_BRIDGE="${CCIP_RELAY_BRIDGE_CHAIN138_WETH:-}"
GAS_138="--legacy --gas-limit 250000 --gas-price 15000000000 --timeout 600"

missing=0
for v in CH138_SEL W9_138 W10_138 MONAD_RELAY_BRIDGE CH138_RELAY_ROUTER CH138_RELAY_BRIDGE MONAD_SEND_BRIDGE; do
  if [[ -z "${!v}" ]]; then
    echo "Missing: $v" >&2
    missing=1
  fi
done
[[ "$missing" == 1 ]] && exit 1

require_private_key_env || exit 1

update_dest() {
  local rpc="$1" bridge="$2" sel="$3" receiver="$4" label="$5" gas="$6"
  local current enabled
  current=$(cast call "$bridge" "destinations(uint64)(uint64,address,bool)" "$sel" --rpc-url "$rpc" 2>/dev/null | awk 'NR==2{print $1}')
  enabled=$(cast call "$bridge" "destinations(uint64)(uint64,address,bool)" "$sel" --rpc-url "$rpc" 2>/dev/null | awk 'NR==3{print $1}')
  if [[ "${enabled,,}" == "true" && "${current,,}" == "${receiver,,}" ]]; then
    echo "Skip (aligned): $label"
    return 0
  fi
  if [[ "$DRY_RUN" == "1" ]]; then
    echo "[DRY RUN] $label: updateDestination($sel,$receiver) on $bridge"
    return 0
  fi
  echo "=== $label ==="
  # shellcheck disable=SC2086
  cast send "$bridge" "updateDestination(uint64,address)" "$sel" "$receiver" \
    --rpc-url "$rpc" --private-key "$PRIVATE_KEY" $gas
}

add_dest() {
  local rpc="$1" bridge="$2" sel="$3" receiver="$4" label="$5" gas="$6"
  local enabled
  enabled=$(cast call "$bridge" "destinations(uint64)(uint64,address,bool)" "$sel" --rpc-url "$rpc" 2>/dev/null | awk 'NR==3{print $1}')
  if [[ "$enabled" == "true" ]]; then
    update_dest "$rpc" "$bridge" "$sel" "$receiver" "$label (update)" "$gas"
    return 0
  fi
  if [[ "$DRY_RUN" == "1" ]]; then
    echo "[DRY RUN] $label: addDestination($sel,$receiver) on $bridge"
    return 0
  fi
  echo "=== $label ==="
  # shellcheck disable=SC2086
  cast send "$bridge" "addDestination(uint64,address)" "$sel" "$receiver" \
    --rpc-url "$rpc" --private-key "$PRIVATE_KEY" $gas
}

authorize_bridge() {
  local rpc="$1" router="$2" bridge="$3"
  local ok
  ok=$(cast call "$router" "authorizedBridges(address)(bool)" "$bridge" --rpc-url "$rpc" 2>/dev/null || echo "false")
  if [[ "$ok" == "true" ]]; then
    echo "Skip: $bridge already authorized on $router"
    return 0
  fi
  if [[ "$DRY_RUN" == "1" ]]; then
    echo "[DRY RUN] authorizeBridge($bridge) on $router"
    return 0
  fi
  echo "=== Authorize 138 WETH relay bridge on router ==="
  cast send "$router" "authorizeBridge(address)" "$bridge" \
    --rpc-url "$rpc" --private-key "$PRIVATE_KEY" $GAS_138
}

echo "=== Wire Monad↔138 relay destinations ==="
update_dest "$RPC138" "$W9_138" "$MONAD_SEL" "$MONAD_RELAY_BRIDGE" "138 WETH9 → Monad relay bridge" "$GAS_138"
update_dest "$RPC138" "$W10_138" "$MONAD_SEL" "$MONAD_RELAY_BRIDGE" "138 WETH10 → Monad relay bridge" "$GAS_138"
add_dest "$MONAD_RPC" "$MONAD_SEND_BRIDGE" "$CH138_SEL" "$CH138_RELAY_BRIDGE" "Monad send → 138 WETH relay bridge" "--legacy --timeout 600"
authorize_bridge "$RPC138" "$CH138_RELAY_ROUTER" "$CH138_RELAY_BRIDGE"
echo "Done."
