#!/usr/bin/env bash
# Admin template: CWMultiTokenBridgeL1.configureDestination for the eight chains
# that are not Mainnet / Avalanche (those typically already wired for cUSDT/cUSDC).
#
# Uses receiver addresses from CW_BRIDGE_OPTIMISM, CW_BRIDGE_CRONOS, CW_BRIDGE_BSC,
# CW_BRIDGE_GNOSIS, CW_BRIDGE_POLYGON, CW_BRIDGE_BASE, CW_BRIDGE_ARBITRUM, CW_BRIDGE_CELO
# (see smom-dbis-138/.env). CCIP selectors match docs/deployment/BRIDGE_CONFIGURATION.md
# and Chainlink CCIP directory — verify before mainnet use.
#
# Target contract: CW_L1_BRIDGE_CHAIN138 — onlyAdmin.
#
# Usage:
#   bash scripts/deployment/cw-l1-configure-destination-8-chains.sh [--print-only]
#   # default writes reports/status/cw-l1-configure-destination-8-chains.cast.sh
#
# Env:
#   CW_L1_BRIDGE_CHAIN138, RPC_URL_138 (or CHAIN138_RPC), PRIVATE_KEY (only if you execute the .sh)
#   CUSDT_ADDRESS_138, CUSDC_ADDRESS_138 (or override CANONICAL_TOKENS="0x...,0x...")
#
# Does not broadcast unless you run the generated file. Never commit private keys.

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"

PRINT_ONLY=0
[[ "${1:-}" == "--print-only" ]] && PRINT_ONLY=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}}}"
OUT_SH="${OUT_SH:-$SMOM_ROOT/reports/status/cw-l1-configure-destination-8-chains.cast.sh}"

if [[ -z "$BRIDGE" || "$BRIDGE" == "0x0000000000000000000000000000000000000000" ]]; then
  echo "Set CW_L1_BRIDGE_CHAIN138 in .env" >&2
  exit 1
fi

# Remaining eight (exclude Mainnet + Avalanche)
declare -a ROWS=(
  "Optimism|3734403246176062136|${CW_BRIDGE_OPTIMISM:-}"
  "Cronos|1456215246176062136|${CW_BRIDGE_CRONOS:-}"
  "BSC|11344663589394136015|${CW_BRIDGE_BSC:-}"
  "Gnosis|465200170687744372|${CW_BRIDGE_GNOSIS:-}"
  "Polygon|4051577828743386545|${CW_BRIDGE_POLYGON:-}"
  "Base|15971525489660198786|${CW_BRIDGE_BASE:-}"
  "Arbitrum|4949039107694359620|${CW_BRIDGE_ARBITRUM:-}"
  "Celo|1346049177634351622|${CW_BRIDGE_CELO:-}"
)

TOKENS_STR="${CANONICAL_TOKENS:-}"
if [[ -z "$TOKENS_STR" ]]; then
  T1="${CUSDT_ADDRESS_138:-${COMPLIANT_USDT_ADDRESS:-}}"
  T2="${CUSDC_ADDRESS_138:-${COMPLIANT_USDC_ADDRESS:-}}"
  TOKENS_STR="${T1},${T2}"
fi

IFS=',' read -r -a TOKEN_ADDRS <<< "$(echo "$TOKENS_STR" | tr -d '[:space:]')"
[[ -n "${TOKEN_ADDRS[0]:-}" && "${TOKEN_ADDRS[0]}" == 0x* ]] || { echo "Set CUSDT_ADDRESS_138 or CANONICAL_TOKENS" >&2; exit 1; }

gen() {
  local sym="$1" token="$2"
  echo ""
  echo "# --- $sym $token ---"
  for row in "${ROWS[@]}"; do
    IFS='|' read -r name sel recv <<< "$row"
    if [[ -z "$recv" || "$recv" == "0x0000000000000000000000000000000000000000" ]]; then
      echo "# SKIP $name: CW_BRIDGE_* receiver unset"
      continue
    fi
    echo "# $sym -> $name (selector $sel) receiver=$recv"
    echo "cast send \"$BRIDGE\" \\"
    echo "  \"configureDestination(address,uint64,address,bool)\" \\"
    echo "  \"$token\" \\"
    echo "  \"$sel\" \\"
    echo "  \"$recv\" \\"
    echo "  true \\"
    echo "  --rpc-url \"$RPC\" \\"
    echo "  --private-key \"\$PRIVATE_KEY\" \\"
    echo "  --legacy \\"
    echo "  --gas-limit 500000"
    echo ""
    echo "# Verify: cast call \"$BRIDGE\" \"destinations(address,uint64)(address,bool)\" \"$token\" \"$sel\" --rpc-url \"$RPC\""
    echo ""
  done
}

{
  echo "#!/usr/bin/env bash"
  echo "# Generated: cw-l1-configure-destination-8-chains.sh"
  echo "# Admin-only: configureDestination on CW L1 for eight chains (not Mainnet/Avalanche)."
  echo "# Requires: admin key = deployer or bridge admin; source .env with PRIVATE_KEY."
  echo "set -euo pipefail"
  echo ""

  if [[ -n "${TOKEN_ADDRS[1]:-}" && "${TOKEN_ADDRS[1]}" == 0x* ]]; then
    gen "cUSDT" "${TOKEN_ADDRS[0]}"
    gen "cUSDC" "${TOKEN_ADDRS[1]}"
  else
    gen "TOKEN" "${TOKEN_ADDRS[0]}"
  fi
} > "$OUT_SH"
chmod +x "$OUT_SH"

echo "Wrote: $OUT_SH"
echo ""
if [[ "$PRINT_ONLY" -eq 1 ]] || [[ -t 1 ]]; then
  cat "$OUT_SH"
fi
