#!/usr/bin/env bash
# Verify deployed contracts on Avalanche, Arbitrum, Cronos.
# Requires ETHERSCAN_API_KEY (or SNOWTRACE_API_KEY, ARBISCAN_API_KEY, CRONOSCAN_API_KEY per chain).

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


if [ -z "${ETHERSCAN_API_KEY:-}" ]; then
  echo "ERROR: ETHERSCAN_API_KEY not set. Set in .env or export."
  exit 1
fi

export SNOWTRACE_API_KEY="${SNOWTRACE_API_KEY:-$ETHERSCAN_API_KEY}"
export ARBISCAN_API_KEY="${ARBISCAN_API_KEY:-$ETHERSCAN_API_KEY}"
# Cronos not in Etherscan V2; needs separate CRONOSCAN_API_KEY from cronoscan.com (no fallback)

verify_one() {
  local addr="$1"
  local contract="$2"
  local chain_arg="$3"
  local chain_name="$4"
  local api_key="${5:-${ETHERSCAN_API_KEY}}"
  echo "Verifying $contract at $addr on $chain_name..."
  # shellcheck disable=SC2086
  forge verify-contract "$addr" "$contract" $chain_arg \
    --etherscan-api-key "${api_key}" \
    --watch 2>/dev/null || echo "  (may already be verified or need chain-specific API key)"
}

# Avalanche 43114
for item in \
  "${WETH9_AVALANCHE:-}:contracts/tokens/WETH.sol:WETH" \
  "${WETH10_AVALANCHE:-}:contracts/tokens/WETH10.sol:WETH10" \
  "${CCIPWETH9_BRIDGE_AVALANCHE:-}:contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge" \
  "${CCIPWETH10_BRIDGE_AVALANCHE:-}:contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge"; do
  addr="${item%%:*}"
  rest="${item#*:}"
  contract="${rest%%:*}"
  if [ -z "$addr" ]; then
    continue
  fi
  verify_one "$addr" "$contract" "--chain avalanche" "Avalanche" "${SNOWTRACE_API_KEY:-$ETHERSCAN_API_KEY}"
done

# Arbitrum 42161
for item in \
  "${WETH9_ARBITRUM:-}:contracts/tokens/WETH.sol:WETH" \
  "${WETH10_ARBITRUM:-}:contracts/tokens/WETH10.sol:WETH10" \
  "${CCIPWETH9_BRIDGE_ARBITRUM:-}:contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge" \
  "${CCIPWETH10_BRIDGE_ARBITRUM:-}:contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge"; do
  addr="${item%%:*}"
  rest="${item#*:}"
  contract="${rest%%:*}"
  if [ -z "$addr" ]; then
    continue
  fi
  verify_one "$addr" "$contract" "--chain arbitrum" "Arbitrum" "${ARBISCAN_API_KEY:-$ETHERSCAN_API_KEY}"
done

# Cronos 25 — API: explorer-api.cronos.org/mainnet. Forge Blockscout verifier incompatible.
# Try: ./scripts/deployment/verify-cronos-contracts.sh (may fail; use manual fallback)
# See docs/04-configuration/CRONOS_EXPLORER_OPERATIONS.md
if [ -n "${CRONOSCAN_API_KEY:-}" ]; then
  echo ""
  echo "Cronos contracts: run ./scripts/deployment/verify-cronos-contracts.sh"
  echo "If that fails, manual verification: export-cronos-verification-sources.sh then CRONOS_VERIFICATION_RUNBOOK.md"
  echo "  WETH9:        0x99B3511A2d315A497C8112C1fdd8D508d4B1E506"
  echo "  WETH10:       0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6"
  echo "  CCIPWETH9:    0x3Cc23d086fCcbAe1e5f3FE2bA4A263E1D27d8Cab"
  echo "  CCIPWETH10:   0x105F8A15b819948a89153505762444Ee9f324684"
  echo ""
else
  echo "Skipping Cronos: set CRONOSCAN_API_KEY (from explorer.cronos.org) for manual verification."
fi

echo "Verification complete."
