#!/usr/bin/env bash
# Verify deployed Trustless Bridge contracts (BondManager, LiquidityPool, Inbox, Lockbox) on each chain.
# Uses dotenv at smom-dbis-138/.env. Requires: cast (Foundry).
# Exit 0 if all checked addresses have code; non-zero if any missing.
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 "${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
[[ -f "$DOTENV" ]] && set -a && source "$DOTENV" && set +a

PASS=0
FAIL=0
FAILED_CHAINS=""

check_addr() {
  local label="$1"
  local addr="$2"
  local rpc="$3"
  if [[ -z "$addr" || "$addr" == "0x"*"..." ]]; then return; fi
  local code
  code=$(cast code "$addr" --rpc-url "$rpc" 2>/dev/null || true)
  if [[ -n "$code" && "${#code}" -gt 2 ]]; then
    echo "  OK $label $addr"
    PASS=$((PASS+1))
    return 0
  else
    echo "  FAIL $label $addr (no code or RPC error)"
    FAIL=$((FAIL+1))
    return 1
  fi
}

verify_chain() {
  local name="$1"
  local rpc="$2"
  shift 2
  if [[ -z "$rpc" ]]; then echo "Skip $name (no RPC)"; return; fi
  echo "--- $name ---"
  local any_fail=0
  while [[ $# -ge 2 ]]; do
    check_addr "$1" "$2" "$rpc" || any_fail=1
    shift 2
  done
  if [[ $any_fail -ne 0 ]]; then FAILED_CHAINS="$FAILED_CHAINS $name"; fi
  echo ""
}

# Mainnet (Ethereum)
verify_chain "Ethereum Mainnet" "${ETHEREUM_MAINNET_RPC:-}" \
  "BOND_MANAGER" "${BOND_MANAGER_MAINNET:-}" \
  "LIQUIDITY_POOL" "${LIQUIDITY_POOL_ETH_MAINNET:-}" \
  "INBOX" "${INBOX_ETH_MAINNET:-}"

# Chain 138
verify_chain "Chain 138" "${RPC_URL_138:-${CHAIN_138_RPC_URL:-}}" \
  "LOCKBOX_138" "${LOCKBOX_138:-}"

# BSC
verify_chain "BSC" "${BSC_RPC_URL:-}" \
  "BOND_MANAGER" "${BSC_BOND_MANAGER:-}" \
  "LIQUIDITY_POOL" "${BSC_LIQUIDITY_POOL_ETH:-}" \
  "INBOX" "${BSC_INBOX_ETH:-}" \
  "LOCKBOX" "${BSC_LOCKBOX:-}"

# Polygon
verify_chain "Polygon" "${POLYGON_MAINNET_RPC:-}" \
  "BOND_MANAGER" "${POLYGON_BOND_MANAGER:-}" \
  "LIQUIDITY_POOL" "${POLYGON_LIQUIDITY_POOL_ETH:-}" \
  "INBOX" "${POLYGON_INBOX_ETH:-}" \
  "LOCKBOX" "${POLYGON_LOCKBOX:-}"

# Base
verify_chain "Base" "${BASE_MAINNET_RPC:-}" \
  "BOND_MANAGER" "${BASE_BOND_MANAGER:-}" \
  "LIQUIDITY_POOL" "${BASE_LIQUIDITY_POOL_ETH:-}" \
  "INBOX" "${BASE_INBOX_ETH:-}" \
  "LOCKBOX" "${BASE_LOCKBOX:-}"

# Optimism
verify_chain "Optimism" "${OPTIMISM_MAINNET_RPC:-}" \
  "BOND_MANAGER" "${OPTIMISM_BOND_MANAGER:-}" \
  "LIQUIDITY_POOL" "${OPTIMISM_LIQUIDITY_POOL_ETH:-}" \
  "INBOX" "${OPTIMISM_INBOX_ETH:-}" \
  "LOCKBOX" "${OPTIMISM_LOCKBOX:-}"

# Cronos
verify_chain "Cronos" "${CRONOS_RPC_URL:-}" \
  "BOND_MANAGER" "${CRONOS_BOND_MANAGER:-}" \
  "LIQUIDITY_POOL" "${CRONOS_LIQUIDITY_POOL_ETH:-}" \
  "INBOX" "${CRONOS_INBOX_ETH:-}" \
  "LOCKBOX" "${CRONOS_LOCKBOX:-}"

# Arbitrum (addresses from deployment run; may not be in .env)
verify_chain "Arbitrum" "${ARBITRUM_MAINNET_RPC:-}" \
  "BOND_MANAGER" "${ARBITRUM_BOND_MANAGER:-0x26Eb0AC01BAb5756c73945d48e2B8e28D6b32287}" \
  "LIQUIDITY_POOL" "${ARBITRUM_LIQUIDITY_POOL_ETH:-0x866aCe8664FF40E0a8842705E0D140E614eCc67B}" \
  "INBOX" "${ARBITRUM_INBOX_ETH:-0xb1061A71412E18F66FF8D043e3F0682532ECdaf7}" \
  "LOCKBOX" "${ARBITRUM_LOCKBOX:-0x57f8b4DB2d92CAB6093405b53158ab3224D37D24}"

# Avalanche
verify_chain "Avalanche" "${AVALANCHE_RPC_URL:-${AVALANCHE_MAINNET_RPC:-}}" \
  "BOND_MANAGER" "${AVALANCHE_BOND_MANAGER:-0xD1695ed8F7683EE5d1BF55ed34585B22F474a7ef}" \
  "LIQUIDITY_POOL" "${AVALANCHE_LIQUIDITY_POOL_ETH:-0xb6D2f38b9015F32ccE8818509c712264E7fceeD3}" \
  "INBOX" "${AVALANCHE_INBOX_ETH:-0x866aCe8664FF40E0a8842705E0D140E614eCc67B}" \
  "LOCKBOX" "${AVALANCHE_LOCKBOX:-0x7e6fB8D80f81430e560F8232b2A4fd06249d74ce}"

# Gnosis
verify_chain "Gnosis" "${GNOSIS_MAINNET_RPC:-}" \
  "BOND_MANAGER" "${GNOSIS_BOND_MANAGER:-0x73376eB92c16977B126dB9112936A20Fa0De3442}" \
  "LIQUIDITY_POOL" "${GNOSIS_LIQUIDITY_POOL_ETH:-0xb689c1C69DAa08DEb5D8feA2aBF0F64bFD409727}" \
  "INBOX" "${GNOSIS_INBOX_ETH:-0x4C38F9A5ed68A04cd28a72E8c68C459Ec34576f3}" \
  "LOCKBOX" "${GNOSIS_LOCKBOX:-0x0CA60e6f8589c540200daC9D9Cb27BC2e48eE66A}"

echo "=============================================="
echo "Summary: $PASS passed, $FAIL failed"
if [[ -n "$FAILED_CHAINS" ]]; then
  echo "Chains with failures:$FAILED_CHAINS"
  exit 1
fi
exit 0
