#!/usr/bin/env bash
# Verify all four Cronos contracts via Etherscan-style API.
# Uses explorer-api.cronos.org (module/action, solidity-standard-json-input).
# Run from smom-dbis-138/

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 "${CRONOSCAN_API_KEY:-}" ]; then
  echo "ERROR: CRONOSCAN_API_KEY not set. Set in .env (from explorer.cronos.org/register)."
  exit 1
fi

export CRONOSCAN_API_KEY

echo "Cronos verification (Etherscan-style API)"
echo "  API: https://explorer-api.cronos.org/mainnet/api"
echo "  Chain: cronos (from foundry.toml)"
echo ""

verify() {
  local addr="$1"
  local contract="$2"
  local extra_args="${3:-}"
  echo "Verifying $contract at $addr..."
  # shellcheck disable=SC2086
  if forge verify-contract \
    "$addr" "$contract" \
    --chain cronos \
    --etherscan-api-key "$CRONOSCAN_API_KEY" \
    --skip-is-verified-check \
    $extra_args \
    --watch 2>&1; then
    echo "  ✓ $contract verified"
  else
    echo "  ✗ $contract verification failed"
    return 1
  fi
}

FAIL=0

# WETH9 - no constructor args
verify "0x99B3511A2d315A497C8112C1fdd8D508d4B1E506" "contracts/tokens/WETH.sol:WETH" || FAIL=$((FAIL+1))

# WETH10 - no constructor args
verify "0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6" "contracts/tokens/WETH10.sol:WETH10" || FAIL=$((FAIL+1))

# CCIPWETH9Bridge - constructor(router, weth9, linkToken)
verify "0x3Cc23d086fCcbAe1e5f3FE2bA4A263E1D27d8Cab" "contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge" \
  "--constructor-args $(cast abi-encode 'constructor(address,address,address)' 0xE26B0A098D861d5C7d9434aD471c0572Ca6EAa67 0x99B3511A2d315A497C8112C1fdd8D508d4B1E506 0x8c80A01F461f297Df7F9DA3A4f740D7297C8Ac85)" || FAIL=$((FAIL+1))

# CCIPWETH10Bridge - constructor(router, weth10, linkToken)
verify "0x105F8A15b819948a89153505762444Ee9f324684" "contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge" \
  "--constructor-args $(cast abi-encode 'constructor(address,address,address)' 0xE26B0A098D861d5C7d9434aD471c0572Ca6EAa67 0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6 0x8c80A01F461f297Df7F9DA3A4f740D7297C8Ac85)" || FAIL=$((FAIL+1))

echo ""
if [ "$FAIL" -eq 0 ]; then
  echo "All four Cronos contracts verified."
  exit 0
else
  echo "$FAIL contract(s) failed."
  echo ""
  echo "Manual verification (recommended):"
  echo "  1. ./scripts/deployment/export-cronos-verification-sources.sh"
  echo "  2. Open https://explorer.cronos.org/verifyContract"
  echo "  3. Follow docs/deployment/CRONOS_VERIFICATION_RUNBOOK.md"
  echo ""
  exit 1
fi
