#!/usr/bin/env bash
# Deploy CompliantUSDT and CompliantUSDC to each target chain (Ethereum, BSC, Polygon, Base, etc.).
# Uses smom-dbis-138/.env. Requires per-chain RPC and PRIVATE_KEY with gas on each chain.
# Output: suggested .env lines for CUSDT_ADDRESS_<chainId> and CUSDC_ADDRESS_<chainId>.
# See docs/deployment/CUSDT_CUSDC_MULTICHAIN_LIQUIDITY_RUNBOOK.md
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
DOTENV="$REPO_ROOT/.env"
cd "$REPO_ROOT"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "$REPO_ROOT"
elif [[ -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

GNOSIS_MAINNET_RPC="${GNOSIS_MAINNET_RPC:-${GNOSIS_RPC:-}}"
CRONOS_RPC_URL="${CRONOS_RPC_URL:-${CRONOS_RPC:-}}"
CELO_MAINNET_RPC="${CELO_MAINNET_RPC:-${CELO_RPC:-}}"
WEMIX_MAINNET_RPC="${WEMIX_MAINNET_RPC:-${WEMIX_RPC:-}}"

# Chain name : chain_id : RPC env var name
CHAINS=(
  "ETHEREUM:1:ETHEREUM_MAINNET_RPC"
  "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"
  "WEMIX:1111:WEMIX_MAINNET_RPC"
)

FILTER="${DEPLOY_CUSDT_CUSDC_FILTER:-}"
DRY_RUN="${DEPLOY_CUSDT_CUSDC_DRY_RUN:-}"

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

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

  echo "=== $name (chain $chain_id) ==="
  if [[ -n "$DRY_RUN" ]]; then
    echo "[DRY RUN] Would deploy CompliantUSDT and CompliantUSDC on $name via $rpc_var"
    continue
  fi

  CUSDT=""
  CUSDC=""
  log_usdt="/tmp/cusdt-deploy-$chain_id.log"
  log_usdc="/tmp/cusdc-deploy-$chain_id.log"

  if forge script script/DeployCompliantUSDT.s.sol:DeployCompliantUSDT \
    --rpc-url "$rpc" \
    --chain-id "$chain_id" \
    --broadcast \
    --private-key "${PRIVATE_KEY:?PRIVATE_KEY required}" \
    -vv 2>&1 | tee "$log_usdt"; then
    CUSDT=$(grep -oE "CompliantUSDT deployed at: 0x[a-fA-F0-9]{40}" "$log_usdt" | tail -1 | awk '{print $NF}')
  fi
  if [[ -z "$CUSDT" ]]; then
    echo "Warning: CompliantUSDT deploy failed or address not found on $name"
  else
    echo "CUSDT_ADDRESS_${chain_id}=$CUSDT"
  fi

  if forge script script/DeployCompliantUSDC.s.sol:DeployCompliantUSDC \
    --rpc-url "$rpc" \
    --chain-id "$chain_id" \
    --broadcast \
    --private-key "${PRIVATE_KEY:?PRIVATE_KEY required}" \
    -vv 2>&1 | tee "$log_usdc"; then
    CUSDC=$(grep -oE "CompliantUSDC deployed at: 0x[a-fA-F0-9]{40}" "$log_usdc" | tail -1 | awk '{print $NF}')
  fi
  if [[ -z "$CUSDC" ]]; then
    echo "Warning: CompliantUSDC deploy failed or address not found on $name"
  else
    echo "CUSDC_ADDRESS_${chain_id}=$CUSDC"
  fi

  echo ""
done

echo "Done. Add the printed CUSDT_ADDRESS_<id> and CUSDC_ADDRESS_<id> lines to .env."
echo "See docs/deployment/CUSDT_CUSDC_MULTICHAIN_LIQUIDITY_RUNBOOK.md for next steps (PMM, Uniswap, Balancer, Curve)."
