#!/usr/bin/env bash
# Register GRU v2 (CompliantFiatTokenV2) addresses on CWMultiTokenBridgeL1 and
# configureDestination for the same 10 CCIP lanes as cUSDT/cUSDC (receivers from CW_BRIDGE_*).
#
# Note: The CWMultiTokenBridgeL1 deployed at CW_L1_BRIDGE_CHAIN138 on Chain 138 does **not**
# include configureSupportedCanonicalToken in runtime bytecode (older build). Do not send that
# call — it always reverts. Destinations + lockAndSend are the supported path.
#
# Usage:
#   bash scripts/deployment/cw-l1-bootstrap-gru-v2-ccip-routes.sh [--dry-run]
#
# Env: PROJECT_ROOT load via proxmox scripts/lib/load-project-env.sh or smom-dbis-138/.env
#      CW_L1_BRIDGE_CHAIN138, RPC_URL_138, PRIVATE_KEY (deployer = bridge admin)
#      CW_BRIDGE_MAINNET, CW_BRIDGE_OPTIMISM, CW_BRIDGE_CRONOS, CW_BRIDGE_BSC,
#      CW_BRIDGE_GNOSIS, CW_BRIDGE_POLYGON, CW_BRIDGE_BASE, CW_BRIDGE_ARBITRUM,
#      CW_BRIDGE_CELO, CW_BRIDGE_AVALANCHE

set -euo pipefail

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

DRY_RUN=0
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=1

if [[ -f "$PROXMOX_ROOT/scripts/lib/load-project-env.sh" ]]; then
  # shellcheck disable=SC1090
  PROJECT_ROOT="$PROXMOX_ROOT" source "$PROXMOX_ROOT/scripts/lib/load-project-env.sh"
elif [[ -f .env ]]; then
  set -a && source .env && set +a
fi

BRIDGE="${CW_L1_BRIDGE_CHAIN138:-}"
RPC="${RPC_URL_138:-${CHAIN138_RPC:-${RPC_URL:-http://192.168.11.211:8545}}}"

[[ -n "$BRIDGE" && "$BRIDGE" != "0x0000000000000000000000000000000000000000" ]] || {
  echo "Set CW_L1_BRIDGE_CHAIN138" >&2
  exit 1
}
[[ -n "${PRIVATE_KEY:-}" ]] || { echo "PRIVATE_KEY required" >&2; exit 1; }

# GRU v2 on Chain 138 (wallet-token-submissions.chain138.json)
V2=(
  "0x243e6581Dc8a98d98B92265858b322b193555C81"
  "0x2bAFA83d8fF8BaE9505511998987D0659791605B"
  "0x707508D223103f5D2d9EFBc656302c9d48878b29"
  "0xee17c18E10E55ce23F7457D018aAa2Fb1E64B281"
  "0xfb37aFd415B70C5cEDc9bA58a72D517207b769Bb"
  "0x2c751bBE4f299b989b3A8c333E0A966cdcA6Fd98"
  "0x60B7FB8e0DD0Be8595AD12Fe80AE832861Be747c"
  "0xe799033c87fE0CE316DAECcefBE3134CC74b76a9"
  "0xF0F0F81bE3D033D8586bAfd2293e37eE2f615647"
  "0x89477E982847023aaB5C3492082cd1bB4b1b9Ef1"
)

# selector|receiver (must match c138-cw-bridge-75-split.sh / BRIDGE_CONFIGURATION)
declare -a ROUTES=(
  "5009297550715157269|${CW_BRIDGE_MAINNET:-}"
  "3734403246176062136|${CW_BRIDGE_OPTIMISM:-}"
  "1456215246176062136|${CW_BRIDGE_CRONOS:-}"
  "11344663589394136015|${CW_BRIDGE_BSC:-}"
  "465200170687744372|${CW_BRIDGE_GNOSIS:-}"
  "4051577828743386545|${CW_BRIDGE_POLYGON:-}"
  "15971525489660198786|${CW_BRIDGE_BASE:-}"
  "4949039107694359620|${CW_BRIDGE_ARBITRUM:-}"
  "1346049177634351622|${CW_BRIDGE_CELO:-}"
  "6433500567565415381|${CW_BRIDGE_AVALANCHE:-}"
)

send() {
  if [[ "$DRY_RUN" -eq 1 ]]; then
    echo "[dry-run] $*"
    return 0
  fi
  "$@"
}

echo "=== configureDestination (10 GRU v2 tokens x up to 10 chains) ==="
for t in "${V2[@]}"; do
  for row in "${ROUTES[@]}"; do
    sel="${row%%|*}"
    recv="${row#*|}"
    if [[ -z "$recv" || "$recv" == "0x0000000000000000000000000000000000000000" ]]; then
      echo "SKIP selector=$sel (receiver unset)"
      continue
    fi
    echo "-> token=$t selector=$sel recv=$recv"
    send cast send "$BRIDGE" "configureDestination(address,uint64,address,bool)" "$t" "$sel" "$recv" true \
      --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 500000
  done
done

echo ""
echo "Done. Verify with: cast call $BRIDGE \"destinations(address,uint64)(address,bool)\" <token> <selector> --rpc-url $RPC"
