#!/usr/bin/env bash
# Check Cronos contract deployment and verification status.
# Verification status must be checked manually by visiting each explorer URL;
# this script confirms on-chain deployment and prints links.

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}"
CONTRACTS=(
  "0x99B3511A2d315A497C8112C1fdd8D508d4B1E506:WETH9"
  "0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6:WETH10"
  "0x3Cc23d086fCcbAe1e5f3FE2bA4A263E1D27d8Cab:CCIPWETH9Bridge"
  "0x105F8A15b819948a89153505762444Ee9f324684:CCIPWETH10Bridge"
)

echo "=== Cronos (Chain 25) contract status ==="
echo ""
echo "On-chain deployment:"
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 — DEPLOYED"
  else
    echo "  ✗ $name — MISSING"
  fi
done

echo ""
echo "Verification status (check each link manually):"
echo "  Verified contracts show 'Contract Source Code Verified' or display source."
echo ""
for entry in "${CONTRACTS[@]}"; do
  addr="${entry%%:*}"
  name="${entry##*:}"
  echo "  $name: https://explorer.cronos.org/address/$addr"
done
echo ""
echo "Run ./scripts/deployment/export-cronos-verification-sources.sh and follow"
echo "docs/deployment/CRONOS_VERIFICATION_RUNBOOK.md to verify unverified contracts."
