#!/usr/bin/env bash
# Deploy Trustless Bridge stack (BondManager, ChallengeManager, LiquidityPool, Inbox, optional Lockbox) on each L2.
# Uses .env for RPC and PRIVATE_KEY. Chains via --chain bsc polygon ...; Lockbox via --lockbox / --no-lockbox (not .env).
#
# Usage:
#   ./scripts/deployment/deploy-trustless-l2s.sh
#   ./scripts/deployment/deploy-trustless-l2s.sh --chain bsc polygon --lockbox
#   ./scripts/deployment/deploy-trustless-l2s.sh --no-lockbox
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
DOTENV="$REPO_ROOT/.env"
cd "$REPO_ROOT"

source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
source "$SCRIPT_DIR/../lib/deployment/prompts.sh"
load_deployment_env

parse_lockbox_tag "$@"
set -- "${PARSE_LOCKBOX_REMAINING[@]}"
parse_chain_filter "$@"
# Backward compat: single positional chain (e.g. deploy-trustless-l2s.sh BSC)
if [[ ${#CHAIN_FILTER[@]} -eq 0 && ${#PARSE_CHAIN_FILTER_REMAINING[@]} -eq 1 ]]; then
  n="$(normalize_chain_name "${PARSE_CHAIN_FILTER_REMAINING[0]}")"
  [[ -n "$n" ]] && CHAIN_FILTER=("$n")
fi

get_rpc() { local n="$1"; case "$n" in BSC) echo "${BSC_RPC_URL:-}";; POLYGON) echo "${POLYGON_MAINNET_RPC:-}";; BASE) echo "${BASE_MAINNET_RPC:-}";; OPTIMISM) echo "${OPTIMISM_MAINNET_RPC:-}";; ARBITRUM) echo "${ARBITRUM_MAINNET_RPC:-}";; AVALANCHE) echo "${AVALANCHE_RPC_URL:-}";; CRONOS) echo "${CRONOS_RPC_URL:-}";; GNOSIS) echo "${GNOSIS_MAINNET_RPC:-}";; *) echo "";; esac; }
get_weth() { local n="$1"; local v="${n}_WETH_ADDRESS"; if [[ -n "${!v:-}" ]]; then echo "${!v}"; return; fi; case "$n" in BSC) echo "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";; POLYGON) echo "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270";; BASE) echo "0x4200000000000000000000000000000000000006";; OPTIMISM) echo "0x4200000000000000000000000000000000000006";; ARBITRUM) echo "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1";; AVALANCHE) echo "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7";; CRONOS) echo "0x5C7F8A570d578ED84E63fdFA7b1eE72dEae1AE23";; GNOSIS) echo "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d";; *) echo "";; esac; }
get_chain_id() { case "$1" in BSC) echo 56;; POLYGON) echo 137;; BASE) echo 8453;; OPTIMISM) echo 10;; ARBITRUM) echo 42161;; AVALANCHE) echo 43114;; CRONOS) echo 25;; GNOSIS) echo 100;; *) echo 0;; esac; }

for name in BSC POLYGON BASE OPTIMISM ARBITRUM AVALANCHE CRONOS GNOSIS; do
  if [[ ${#CHAIN_FILTER[@]} -gt 0 ]] && [[ ! " ${CHAIN_FILTER[*]} " =~ " $name " ]]; then continue; fi
  rpc="$(get_rpc "$name")"
  if [[ -z "$rpc" ]]; then echo "Skip $name: RPC not set"; continue; fi
  weth="$(get_weth "$name")"
  chain_id="$(get_chain_id "$name")"
  echo "=== Deploying Trustless stack on $name (chain $chain_id) ==="
  TRUSTLESS_WETH_ADDRESS="$weth" TRUSTLESS_DEPLOY_LOCKBOX="${TRUSTLESS_DEPLOY_LOCKBOX:-0}" \
  forge script script/bridge/trustless/DeployTrustlessBridge.s.sol:DeployTrustlessBridge \
    --rpc-url "$rpc" --chain-id "$chain_id" --broadcast --private-key "$PRIVATE_KEY" --slow -vvv || true
  echo ""
done

echo "Done. Update .env with BondManager, ChallengeManager, LiquidityPool, Inbox (and Lockbox) per chain."
