#!/usr/bin/env bash
# Deploy CCIP WETH9/WETH10 bridges on Gnosis, Cronos, Celo, Wemix (config-ready chains).
# Requires: PRIVATE_KEY in .env; per-chain RPC and gas (xDAI, CRO, CELO, WEMIX).
# Set CCIP_ROUTER_<CHAIN>, LINK_TOKEN_<CHAIN>; set WETH9_<CHAIN> and WETH10_<CHAIN> (native wrapped token on each chain, or deployment will fail).
# See docs/deployment/ENV_CONFIG_READY_CHAINS.example and docs/07-ccip/CONFIG_READY_CHAINS_COMPLETION_RUNBOOK.md.
#
# Usage: cd smom-dbis-138 && ./scripts/deployment/deploy-bridges-config-ready-chains.sh [gnosis|cronos|celo|wemix|all]
#        PREFLIGHT=1 ...   # run preflight first (default: 1)
#        DRY_RUN=1 ...    # print forge commands only, no deploy
#        SIMULATE=1 ...   # run forge without --broadcast (test script, no on-chain tx)

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_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 "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

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

DRY_RUN="${DRY_RUN:-0}"
SIMULATE="${SIMULATE:-0}"
PREFLIGHT="${PREFLIGHT:-1}"
CHAIN="${1:-all}"

# Per-chain env for DeployWETHBridges: RPC_URL, CCIP_ROUTER_ADDRESS, LINK_TOKEN_ADDRESS, WETH9_ADDRESS, WETH10_ADDRESS (0x0 to skip)
run_deploy() {
  local chain_name="$1"
  local rpc_var="$2"
  local router_var="$3"
  local link_var="$4"
  local weth9_var="$5"
  local weth10_var="$6"
  local rpc="${!rpc_var:-}"
  local router="${!router_var:-}"
  local link="${!link_var:-}"
  local weth9="${!weth9_var:-}"
  local weth10="${!weth10_var:-}"

  if [[ -z "$rpc" || -z "$router" ]]; then
    echo "  Skip $chain_name: set ${rpc_var} and ${router_var} in .env"
    return 0
  fi
  if [[ -z "${PRIVATE_KEY:-}" ]]; then
    echo "  Skip $chain_name: PRIVATE_KEY not set"
    return 0
  fi
  if [[ -z "$weth9" || -z "$weth10" ]]; then
    echo "  Skip $chain_name: set ${weth9_var} and ${weth10_var} in .env (native wrapped token addresses from chain)"
    return 0
  fi

  export RPC_URL="$rpc"
  export CCIP_ROUTER_ADDRESS="$router"
  export LINK_TOKEN_ADDRESS="${link:-0x0000000000000000000000000000000000000000}"
  export WETH9_ADDRESS="$weth9"
  export WETH10_ADDRESS="$weth10"

  local broadcast="--broadcast"
  [[ "$SIMULATE" == "1" ]] && broadcast=""
  local cmd="forge script script/deploy/bridge/DeployWETHBridges.s.sol:DeployWETHBridges --rpc-url \"$RPC_URL\" $broadcast --legacy -vvvv"
  if [[ "$DRY_RUN" == "1" ]]; then
    echo "  [DRY RUN] $chain_name: $cmd"
    return 0
  fi
  if [[ "$SIMULATE" == "1" ]]; then
    echo "  Simulating deploy on $chain_name (no broadcast)..."
  else
    echo "  Deploying bridges on $chain_name..."
  fi
  eval "$cmd" || { echo "  Failed (non-fatal)"; return 0; }
  echo "  Set CCIPWETH9_BRIDGE_${chain_name^^} and CCIPWETH10_BRIDGE_${chain_name^^} in .env from script output."
}

# Preflight (RPC + gas balance check)
if [[ "$PREFLIGHT" == "1" && "$DRY_RUN" != "1" ]]; then
  if ! "$SCRIPT_DIR/preflight-config-ready-chains.sh" "$CHAIN" 2>/dev/null; then
    echo ""
    echo "Preflight failed. Fix RPCs and gas balances, or run with PREFLIGHT=0 to skip."
    exit 1
  fi
  echo ""
fi

echo "=== Deploy CCIP bridges (Gnosis, Cronos, Celo, Wemix) ==="
echo "  DRY_RUN=$DRY_RUN  SIMULATE=$SIMULATE  PREFLIGHT=$PREFLIGHT  CHAIN=$CHAIN"
echo "  Gas tokens: Gnosis=xDAI, Cronos=CRO, Celo=CELO, Wemix=WEMIX"
echo ""

case "$CHAIN" in
  gnosis) run_deploy "GNOSIS" "GNOSIS_RPC" "CCIP_ROUTER_GNOSIS" "LINK_TOKEN_GNOSIS" "WETH9_GNOSIS" "WETH10_GNOSIS" ;;
  cronos) run_deploy "CRONOS" "CRONOS_RPC" "CCIP_ROUTER_CRONOS" "LINK_TOKEN_CRONOS" "WETH9_CRONOS" "WETH10_CRONOS" ;;
  celo)   run_deploy "CELO"   "CELO_RPC"   "CCIP_ROUTER_CELO"   "LINK_TOKEN_CELO"   "WETH9_CELO"   "WETH10_CELO" ;;
  wemix)  run_deploy "WEMIX"  "WEMIX_RPC"  "CCIP_ROUTER_WEMIX"  "LINK_TOKEN_WEMIX"  "WETH9_WEMIX"  "WETH10_WEMIX" ;;
  all)
    run_deploy "GNOSIS" "GNOSIS_RPC" "CCIP_ROUTER_GNOSIS" "LINK_TOKEN_GNOSIS" "WETH9_GNOSIS" "WETH10_GNOSIS"
    run_deploy "CRONOS" "CRONOS_RPC" "CCIP_ROUTER_CRONOS" "LINK_TOKEN_CRONOS" "WETH9_CRONOS" "WETH10_CRONOS"
    run_deploy "CELO"   "CELO_RPC"   "CCIP_ROUTER_CELO"   "LINK_TOKEN_CELO"   "WETH9_CELO"   "WETH10_CELO"
    run_deploy "WEMIX"  "WEMIX_RPC"  "CCIP_ROUTER_WEMIX"  "LINK_TOKEN_WEMIX"  "WETH9_WEMIX"  "WETH10_WEMIX"
    ;;
  *) echo "Usage: $0 [gnosis|cronos|celo|wemix|all]"; exit 1 ;;
esac

echo ""
echo "Next: Add deployed bridge addresses to .env (CCIPWETH9_BRIDGE_*, CCIPWETH10_BRIDGE_*), then run complete-config-ready-chains.sh"
