/**
 * Example: Reverse lookup — resolve a wallet address to its .vns name
 *
 * Run: npx ts-node examples/reverse-lookup.ts
 */

import {
  reverseResolveVNS,
  getProfileByAddress,
  resolver,
  formatAddressOrVNS,
} from '../src/index.js';

async function main() {
  console.log('Vaultfire Name Service — Reverse Lookup\n');

  // Known Vaultfire agent addresses
  const addresses = [
    '0xfA15Ee28939B222B0448261A22156070f0A7813C', // Vaultfire deployer
    '0xc48e2fD9D51b94eBF4965711DD1caa25070c95dB', // vaultfire-sentinel
    '0x144C3630a0ebcD59Bd67813A69Ab1034A8386BAB', // embris-research
    '0x0000000000000000000000000000000000000001', // unknown address
  ];

  // Using reverseResolveVNS — returns just the name string
  console.log('Simple reverse resolution:\n');
  for (const address of addresses) {
    const truncated = `${address.slice(0, 8)}...${address.slice(-4)}`;
    const vnsName = await reverseResolveVNS(address);
    console.log(`  ${truncated} → ${vnsName ?? 'no VNS name'}`);
  }

  // Using getProfileByAddress — returns full profile
  console.log('\nFull profile lookup:\n');
  const address = '0xc48e2fD9D51b94eBF4965711DD1caa25070c95dB';
  const profile = await getProfileByAddress(address);

  if (profile) {
    console.log(`Profile for ${address}:`);
    console.log(`  Name:        ${profile.fullName}`);
    console.log(`  Type:        ${profile.identityType}`);
    console.log(`  Chain:       ${profile.chain}`);
    if (profile.bondTier) {
      console.log(`  Bond Tier:   ${profile.bondTier}`);
    }
    if (profile.hasBond !== undefined) {
      console.log(`  Has Bond:    ${profile.hasBond}`);
    }
  } else {
    console.log(`No VNS profile found for ${address}`);
  }

  // Using resolver class batch reverse
  console.log('\nBatch reverse lookup:\n');
  const knownAddresses = [
    '0xfA15Ee28939B222B0448261A22156070f0A7813C',
    '0xc48e2fD9D51b94eBF4965711DD1caa25070c95dB',
    '0x144C3630a0ebcD59Bd67813A69Ab1034A8386BAB',
  ];
  const names = await resolver.batchReverseResolve(knownAddresses);
  knownAddresses.forEach((addr, i) => {
    console.log(`  ${formatAddressOrVNS(addr, names[i])} — ${addr}`);
  });
}

main().catch(console.error);
