#!/usr/bin/env bash
# Run all explorer verification entrypoints: Chain 138 Blockscout, Ethereum, multichain cW*,
# Avax/Arb bridges, optional Cronos CCIP, optional CCIPLogger (Hardhat), optional Wemix.
# Steps are best-effort (continue on failure); check log for FAILED lines.
#
# Usage: cd smom-dbis-138 && ./scripts/deployment/verify-all-networks-explorers.sh
set -uo pipefail
set +e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SMOM="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Parent of smom-dbis-138 = proxmox workspace root
PROXMOX="$(cd "$SMOM/.." && pwd)"

STEPS_FAILED=0
step() {
  local title="$1"
  shift
  echo ""
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo "$title"
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  if "$@"; then
    echo "[ok] $title"
  else
    echo "[fail] $title (exit $?)" >&2
    STEPS_FAILED=$((STEPS_FAILED + 1))
  fi
}

# Load env for keys
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${SMOM}"
fi

if [[ -f "$PROXMOX/scripts/verify/run-contract-verification-with-proxy.sh" ]]; then
  step "Chain 138 Blockscout (forge proxy)" bash "$PROXMOX/scripts/verify/run-contract-verification-with-proxy.sh" || true
  if [[ -f "$PROXMOX/scripts/verify/verify-dbis-rail-blockscout.sh" ]]; then
    step "Chain 138 DBIS Rail Blockscout" bash "$PROXMOX/scripts/verify/verify-dbis-rail-blockscout.sh" || true
  fi
else
  echo "WARN: Proxmox verify proxy not found at $PROXMOX/scripts/verify/" >&2
fi

step "Ethereum Mainnet CCIP bridges" bash "$SCRIPT_DIR/verify-mainnet-etherscan.sh" || true
step "Ethereum Mainnet CWMultiTokenBridgeL2" bash "$SCRIPT_DIR/verify-mainnet-cw-bridge-etherscan.sh" || true
step "Ethereum Mainnet cW* (CompliantWrappedToken)" bash "$SCRIPT_DIR/verify-mainnet-cw-etherscan.sh" || true
step "Multichain cW* (all CW*_* except MAINNET)" bash "$SCRIPT_DIR/verify-multichain-cw-etherscan.sh" || true
step "Avalanche + Arbitrum WETH/CCIP bridges" bash "$SCRIPT_DIR/verify-deployed-contracts.sh" || true
step "Monad CCIP bridges (chain 143)" bash "$SCRIPT_DIR/verify-monad-ccip-bridges.sh" || true

# Bytecode + manual-verify instructions (no explorer API key required).
step "Cronos deployments + manual verify runbook" bash "$SCRIPT_DIR/verify-cronos-contracts.sh" || true

if command -v npx &>/dev/null && [[ -f "$SMOM/hardhat.config.ts" || -f "$SMOM/hardhat.config.js" ]]; then
  step "CCIPLogger (Hardhat verify, multichain)" bash "$SCRIPT_DIR/verify-ccip-logger-other-chains.sh" || true
else
  echo "Skip CCIPLogger: npx/hardhat config not available"
fi

if [[ -n "${WEMIXSCAN_API_KEY:-}" && -n "${WEMIX_RPC:-}" && -n "${CCIPWETH9_BRIDGE_WEMIX:-}" ]]; then
  step "Wemix CCIP bridges" bash "$SCRIPT_DIR/verify-wemix-bridges.sh" || true
else
  echo "Skip Wemix: WEMIXSCAN_API_KEY / WEMIX_RPC / CCIPWETH9_BRIDGE_WEMIX not all set"
fi

echo ""
echo "=== verify-all-networks-explorers finished: step failures=$STEPS_FAILED ==="
exit 0
