/**
 * Example: Resolve a .vns name to a wallet address
 *
 * Run: npx ts-node examples/resolve-name.ts
 */

import {
  resolveVNSName,
  resolver,
  formatVNSName,
  getIdentityTypeLabel,
  getIdentityTypeColor,
} from '../src/index.js';

async function main() {
  const namesToLookup = [
    'vaultfire-sentinel',
    'embris-research',
    'flourishing-oracle',
  ];

  console.log('Vaultfire Name Service — Name Resolution\n');
  console.log('VNS Contract: 0x1437c4081233A4f0B6907dDf5374Ed610cBD6B25 (Base mainnet)\n');

  for (const name of namesToLookup) {
    const fullName = formatVNSName(name);
    console.log(`Resolving ${fullName}...`);

    const profile = await resolveVNSName(fullName);

    if (!profile) {
      console.log(`  → Not registered\n`);
      continue;
    }

    console.log(`  Address:       ${profile.address}`);
    console.log(`  Identity Type: ${getIdentityTypeLabel(profile.identityType)} (${getIdentityTypeColor(profile.identityType)})`);
    console.log(`  Chain:         ${profile.chain}`);

    if (profile.registeredAt) {
      console.log(`  Registered:    ${new Date(profile.registeredAt).toISOString()}`);
    }

    if (profile.bondTier) {
      console.log(`  Bond Tier:     ${profile.bondTier}`);
    }

    if (profile.specializations?.length) {
      console.log(`  Specializations: ${profile.specializations.join(', ')}`);
    }

    if (profile.explorerUrl) {
      console.log(`  Explorer:      ${profile.explorerUrl}`);
    }

    console.log('');
  }

  // Using the resolver class
  console.log('Using VNSResolver class:\n');
  const profile = await resolver.resolve('vaultfire-sentinel');
  if (profile) {
    console.log(`Resolved vaultfire-sentinel.vns:`);
    console.log(`  Address: ${profile.address}`);
    console.log(`  Type:    ${profile.identityType}`);
  }

  // Batch resolution
  console.log('\nBatch resolving multiple names...');
  const profiles = await resolver.batchResolve([
    'vaultfire-sentinel',
    'embris-research',
    'nonexistent-name-xyz',
  ]);

  profiles.forEach((p, i) => {
    const names = ['vaultfire-sentinel', 'embris-research', 'nonexistent-name-xyz'];
    console.log(`  ${names[i]}: ${p ? p.address : 'not found'}`);
  });
}

main().catch(console.error);
