#!/usr/bin/env bash
# Cronos (chain 25): confirm deployments and prepare manual verification.
#
# Cronos Explorer moved off legacy Etherscan-compatible POST endpoints on the
# public /mainnet/api route (404 on contract verification). Etherscan multichain
# v2 does not list Cronos chain id 25. Foundry forge verify-contract therefore
# cannot submit verification to Cronos reliably from CI.
#
# This script: (1) checks bytecode on RPC, (2) optionally exports Standard-JSON
# inputs for the web UI, (3) prints links to the manual runbook.
#
# Run from smom-dbis-138/:  ./scripts/deployment/verify-cronos-contracts.sh
#
# Optional: CRONOS_EXPORT_SOURCES=1  — run export-cronos-verification-sources.sh
# Optional: CRONOS_REQUIRE_BYTECODE=1 — exit 1 if any listed address has no code (default: 1)

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

RPC="${CRONOS_RPC_URL:-https://evm.cronos.org}"
REQUIRE_CODE="${CRONOS_REQUIRE_BYTECODE:-1}"

echo "Cronos verification (Chain 25)"
echo "  RPC: $RPC"
echo "  Explorer: https://explorer.cronos.org"
echo "  Manual UI: https://explorer.cronos.org/verifyContract"
echo ""
echo "Note: Automated forge/etherscan verification is not supported against the current"
echo "  Cronos Explorer API (see docs/04-configuration/CRONOS_EXPLORER_OPERATIONS.md)."
echo ""

CONTRACTS=(
  "0x99B3511A2d315A497C8112C1fdd8D508d4B1E506:WETH9"
  "0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6:WETH10"
  "0x3Cc23d086fCcbAe1e5f3FE2bA4A263E1D27d8Cab:CCIPWETH9Bridge"
  "0x105F8A15b819948a89153505762444Ee9f324684:CCIPWETH10Bridge"
)

FAIL=0
echo "On-chain bytecode:"
for entry in "${CONTRACTS[@]}"; do
  addr="${entry%%:*}"
  name="${entry##*:}"
  code=$(cast code "$addr" --rpc-url "$RPC" 2>/dev/null || echo "0x")
  if [[ "${#code}" -gt 10 ]]; then
    echo "  ✓ $name $addr"
  else
    echo "  ✗ $name $addr — no bytecode"
    FAIL=$((FAIL + 1))
  fi
done
echo ""

if [[ "${CRONOS_EXPORT_SOURCES:-0}" == "1" ]] && [[ -x "$SCRIPT_DIR/export-cronos-verification-sources.sh" ]]; then
  echo "Regenerating Standard-JSON inputs (.cronos-verify/)..."
  bash "$SCRIPT_DIR/export-cronos-verification-sources.sh" || true
  echo ""
fi

echo "Next steps (manual verification required):"
echo "  1. docs/deployment/CRONOS_VERIFICATION_RUNBOOK.md"
echo "  2. Or: ./scripts/deployment/export-cronos-verification-sources.sh"
echo "     then upload each *_standard_input.json at https://explorer.cronos.org/verifyContract"
echo ""

if [[ "$REQUIRE_CODE" == "1" ]] && [[ "$FAIL" -gt 0 ]]; then
  echo "ERROR: $FAIL contract(s) missing bytecode on $RPC" >&2
  exit 1
fi

echo "verify-cronos-contracts.sh finished (deployment OK; verification is manual on Cronos)."
exit 0
