#!/usr/bin/env bash
# Deploy DODOPMMIntegration on each L2/public chain (BSC, Polygon, Base, Optimism, Arbitrum, Avalanche, Cronos, Gnosis, Celo).
# Uses .env for RPC and token addresses. Chains via tag: --chain bsc polygon ... (or DEPLOY_PMM_L2S_FILTER in .env).
# Usage: ./scripts/deployment/deploy-pmm-all-l2s.sh [--chain bsc polygon base]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$REPO_ROOT"

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

first_set_env() {
  local key
  for key in "$@"; do
    if [[ -n "${!key:-}" ]]; then
      printf '%s' "${!key}"
      return 0
    fi
  done
  return 1
}

parse_chain_filter "$@"
if [[ ${#CHAIN_FILTER[@]} -eq 0 && -n "${DEPLOY_PMM_L2S_FILTER:-}" ]]; then
  CHAIN_FILTER=()
  for n in $DEPLOY_PMM_L2S_FILTER; do n="$(normalize_chain_name "$n")"; [[ -n "$n" ]] && CHAIN_FILTER+=("$n"); done
fi

CHAINS=(
  "BSC:56:BSC_RPC_URL"
  "POLYGON:137:POLYGON_MAINNET_RPC"
  "BASE:8453:BASE_MAINNET_RPC"
  "OPTIMISM:10:OPTIMISM_MAINNET_RPC"
  "ARBITRUM:42161:ARBITRUM_MAINNET_RPC"
  "AVALANCHE:43114:AVALANCHE_RPC_URL"
  "CRONOS:25:CRONOS_RPC_URL"
  "GNOSIS:100:GNOSIS_MAINNET_RPC"
  "CELO:42220:CELO_MAINNET_RPC"
)

for entry in "${CHAINS[@]}"; do
  IFS=: read -r name chain_id rpc_var <<< "$entry"
  if [[ ${#CHAIN_FILTER[@]} -gt 0 ]] && [[ ! " ${CHAIN_FILTER[*]} " =~ " $name " ]]; then continue; fi

  rpc="${!rpc_var:-}"
  if [[ -z "$rpc" ]]; then
    echo "Skip $name (chain $chain_id): $rpc_var not set"
    continue
  fi

  # Prefer chain-prefixed DVM and tokens; fallback to global
  dvm_var="${name}_DODO_VENDING_MACHINE_ADDRESS"
  usdt_var="${name}_OFFICIAL_USDT_ADDRESS"
  usdc_var="${name}_OFFICIAL_USDC_ADDRESS"
  usdt_var_alt="OFFICIAL_USDT_${name}"
  usdc_var_alt="OFFICIAL_USDC_${name}"
  cusdt_var="${name}_COMPLIANT_USDT_ADDRESS"
  cusdc_var="${name}_COMPLIANT_USDC_ADDRESS"
  cusdt_var_alt="COMPLIANT_USDT_${name}"
  cusdc_var_alt="COMPLIANT_USDC_${name}"
  cwusdt_var="CWUSDT_${name}"
  cwusdc_var="CWUSDC_${name}"
  # Per-chain cUSDT/cUSDC (optional): CUSDT_ADDRESS_<chainId> / CUSDC_ADDRESS_<chainId> or POLYGON_COMPLIANT_USDT_ADDRESS etc.
  cusdt_chain="CUSDT_ADDRESS_${chain_id}"
  cusdc_chain="CUSDC_ADDRESS_${chain_id}"
  dvm="$(first_set_env "$dvm_var" "DODO_VENDING_MACHINE_ADDRESS" || true)"
  usdt="$(first_set_env "$usdt_var" "$usdt_var_alt" "OFFICIAL_USDT_ADDRESS" || true)"
  usdc="$(first_set_env "$usdc_var" "$usdc_var_alt" "OFFICIAL_USDC_ADDRESS" || true)"
  compliant_usdt="$(first_set_env "$cusdt_var" "$cusdt_var_alt" "$cusdt_chain" "$cwusdt_var" || true)"
  compliant_usdc="$(first_set_env "$cusdc_var" "$cusdc_var_alt" "$cusdc_chain" "$cwusdc_var" || true)"
  compliant_usdt="${compliant_usdt:-$usdt}"
  compliant_usdc="${compliant_usdc:-$usdc}"

  if [[ -z "$dvm" ]] || [[ -z "$usdt" ]] || [[ -z "$usdc" ]]; then
    echo "Skip $name: set ${dvm_var} (or DODO_VENDING_MACHINE_ADDRESS), ${usdt_var}/${usdt_var_alt}, ${usdc_var}/${usdc_var_alt} (or OFFICIAL_USDT/USDC_ADDRESS)"
    continue
  fi

  if [[ -z "${!usdt_var:-}" && -z "${!usdt_var_alt:-}" ]]; then
    echo "WARN $name: using global OFFICIAL_USDT_ADDRESS fallback; set ${usdt_var} or ${usdt_var_alt} for chain-specific safety"
  fi
  if [[ -z "${!usdc_var:-}" && -z "${!usdc_var_alt:-}" ]]; then
    echo "WARN $name: using global OFFICIAL_USDC_ADDRESS fallback; set ${usdc_var} or ${usdc_var_alt} for chain-specific safety"
  fi

  echo "=== Deploying DODOPMMIntegration on $name (chain $chain_id) ==="
  DODO_VENDING_MACHINE_ADDRESS="$dvm" \
  OFFICIAL_USDT_ADDRESS="$usdt" \
  OFFICIAL_USDC_ADDRESS="$usdc" \
  COMPLIANT_USDT_ADDRESS="$compliant_usdt" \
  COMPLIANT_USDC_ADDRESS="$compliant_usdc" \
  forge script script/dex/DeployDODOPMMIntegration.s.sol:DeployDODOPMMIntegration \
    --rpc-url "$rpc" \
    --chain-id "$chain_id" \
    --broadcast \
    --private-key "$PRIVATE_KEY" \
    -vvv || true
  echo ""
done

echo "Done. Update .env with any new DODOPMM_INTEGRATION_<CHAIN> addresses."
