#!/bin/bash
# Verify the CURRENT live contracts on Base mainnet (the addresses
# in agent/src/config.ts that were never verified after the
# most recent redeploy).
#
# Usage: ETHERSCAN_API_KEY=... ./scripts/verify-current.sh

set +e  # don't abort on a single failure — keep going

if [ -z "$ETHERSCAN_API_KEY" ] && [ -z "$BASESCAN_API_KEY" ]; then
  echo "ERROR: set ETHERSCAN_API_KEY (or BASESCAN_API_KEY) before running."
  exit 1
fi

NET="baseMainnet"

DEPLOYER="0xfA15Ee28939B222B0448261A22156070f0A7813C"

# ── Constructor args derived from on-chain state reads ─────────────
HUMAN_TREASURY="0xA054f831B562e729F8D268291EBde1B2EDcFb84F"  # AIAccountabilityBondsV2
ID_REG="0xa7BD20bf5De63df949cA5Be2F20835978eCba81A"          # ERC8004IdentityRegistry
ZK_VERIFIER="0x32211773C8266774A704aeD471f8eF073f34bA79"     # BeliefAttestationVerifier
PBONDS="0x01C479F0c039fEC40c0Cf1c5C921bab457d57441"          # AIPartnershipBondsV2
REP_REG="0x98afd1440B2238D73c1394720277a6d031fCbbD0"
VAL_REG="0x8D3495772EAcAdB7dC09F1784eF06F2D109fb8a5"
PBAV_IMAGE_ID="0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
PBAV_RISCZERO="0x0000000000000000000000000000000000000000"   # Will check on-chain

ZERO="0x0000000000000000000000000000000000000000"

verify() {
  local addr=$1
  local contract=$2
  shift 2
  local args="$@"
  echo ""
  echo "▶ Verifying $contract @ $addr"
  if [ -z "$args" ]; then
    npx hardhat verify --network "$NET" "$addr" 2>&1 | tail -8
  else
    # shellcheck disable=SC2086
    npx hardhat verify --network "$NET" "$addr" $args 2>&1 | tail -8
  fi
}

echo "================================================================"
echo "VERIFYING CURRENT LIVE BASE MAINNET CONTRACTS"
echo "================================================================"

# Parameterless constructors
verify 0xa7BD20bf5De63df949cA5Be2F20835978eCba81A ERC8004IdentityRegistry
verify 0x01C479F0c039fEC40c0Cf1c5C921bab457d57441 AIPartnershipBondsV2
verify 0xf7B2dC73Ec69528F171F4a0ded62da557866BaA5 FlourishingMetricsOracle
verify 0x32211773C8266774A704aeD471f8eF073f34bA79 BeliefAttestationVerifier

# With constructor args
verify 0x6750D28865434344e04e1D0a6044394b726C3dfE AIAccountabilityBondsV2 "$HUMAN_TREASURY"
verify 0x98afd1440B2238D73c1394720277a6d031fCbbD0 ERC8004ReputationRegistry "$ID_REG"
verify 0x8D3495772EAcAdB7dC09F1784eF06F2D109fb8a5 ERC8004ValidationRegistry "$ID_REG" "$ZK_VERIFIER"
verify 0x8bEFe7411AecddA6E1Aea8F031fedD293Ca7372E VaultfireERC8004Adapter "$PBONDS" "$ID_REG" "$REP_REG" "$VAL_REG"
verify 0x2a46B5932e9559176241056A04a608B8fEbCc5A7 ProductionBeliefAttestationVerifier "$DEPLOYER" "$PBAV_IMAGE_ID"

# These need on-chain reads first (handled by separate runner)
echo ""
echo "▶ Skipping (need ctor args from on-chain reads):"
echo "  - MultisigGovernance (signers[], threshold)"
echo "  - DilithiumAttestor (origin, zkEnabled, verifierAddress)"
echo "  - VaultfireTeleporterBridge (messenger, gasLimit)"

echo ""
echo "================================================================"
echo "DONE"
echo "================================================================"
