#!/usr/bin/env bash
# Verify CCIPLogger on Etherscan-V2-supported chains (BSC, Polygon, Gnosis, Optimism, Base, Arbitrum, Avalanche).
# Prerequisite: Deploy CCIPLogger per chain (deploy-ccip-logger-all-chains.sh) and set in .env:
#   CCIP_LOGGER_BSC, CCIP_BSC_ROUTER (and same for polygon, gnosis, optimism, base, arbitrum, avalanche).
# Mainnet: already verified (see COMPLETION_RUN_WITH_KEYS_20260223.md). Cronos: not Etherscan V2; use Cronoscan.
# Usage: cd smom-dbis-138 && ./scripts/deployment/verify-ccip-logger-other-chains.sh
# Requires: ETHERSCAN_API_KEY and PRIVATE_KEY in .env; npm/hardhat.

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

AUTH_SIGNER="${AUTHORIZED_SIGNER:-0x0000000000000000000000000000000000000000}"
SOURCE_CHAIN_SELECTOR="${CHAIN138_SELECTOR:-138}"

# Network -> env suffix (logger address, router). mainnet skipped (already verified); cronos skipped (no V2).
declare -A LOGGER_VAR=(
  [bsc]=CCIP_LOGGER_BSC
  [polygon]=CCIP_LOGGER_POLYGON
  [gnosis]=CCIP_LOGGER_GNOSIS
  [optimism]=CCIP_LOGGER_OPTIMISM
  [base]=CCIP_LOGGER_BASE
  [arbitrum]=CCIP_LOGGER_ARBITRUM
  [avalanche]=CCIP_LOGGER_AVALANCHE
)
declare -A ROUTER_VAR=(
  [bsc]=CCIP_BSC_ROUTER
  [polygon]=CCIP_POLYGON_ROUTER
  [gnosis]=CCIP_GNOSIS_ROUTER
  [optimism]=CCIP_OPTIMISM_ROUTER
  [base]=CCIP_BASE_ROUTER
  [arbitrum]=CCIP_ARBITRUM_ROUTER
  [avalanche]=CCIP_AVALANCHE_ROUTER
)

if [[ -z "${ETHERSCAN_API_KEY:-}" ]]; then
  echo "ETHERSCAN_API_KEY not set. Add to .env and re-run." >&2
  exit 1
fi

VERIFIED=0
SKIPPED=0
FAILED=0

for net in bsc polygon gnosis optimism base arbitrum avalanche; do
  addr_var="${LOGGER_VAR[$net]}"
  router_var="${ROUTER_VAR[$net]}"
  addr="${!addr_var:-}"
  router="${!router_var:-}"
  if [[ -z "$addr" || "$addr" = "0x0000000000000000000000000000000000000000" ]]; then
    echo "[$net] Skip (no $addr_var in .env)"
    SKIPPED=$((SKIPPED + 1))
    continue
  fi
  if [[ -z "$router" || "$router" = "0x0000000000000000000000000000000000000000" ]]; then
    echo "[$net] Skip ($addr_var set but $router_var missing)"
    SKIPPED=$((SKIPPED + 1))
    continue
  fi
  echo "[$net] Verifying CCIPLogger $addr ..."
  if npx hardhat verify --network "$net" "$addr" "$router" "$AUTH_SIGNER" "$SOURCE_CHAIN_SELECTOR" 2>&1; then
    echo "[$net] Verified."
    VERIFIED=$((VERIFIED + 1))
  else
    echo "[$net] Verify failed (may already be verified)."
    FAILED=$((FAILED + 1))
  fi
done

echo ""
echo "Summary: $VERIFIED verified, $SKIPPED skipped (no address/router), $FAILED failed."
echo "To deploy first: ./scripts/deployment/deploy-ccip-logger-all-chains.sh then set CCIP_LOGGER_* and CCIP_*_ROUTER in .env."
