import { createShade } from '@shade/sdk';
import { loadConfig } from '../config.js';

export async function fingerprintCommand(address?: string): Promise<void> {
  const config = loadConfig();
  const shade = await createShade({
    prekeyServer: config.prekeyServer,
    storage: config.storage,
    address: config.address,
    autoReplenish: false,
  });

  try {
    if (address) {
      // Peer fingerprint — requires an existing session
      try {
        const fp = await shade.getFingerprintFor(address);
        console.log(`${address}:`);
        console.log(`  ${fp}`);
      } catch {
        console.error(`No session for ${address}. Run \`shade peer add ${address}\` first.`);
        process.exit(1);
      }
    } else {
      const fp = await shade.fingerprint;
      console.log('Your safety number:');
      console.log('');
      console.log(`  ${fp}`);
      console.log('');
      console.log('Compare this with your peer out-of-band to verify no MITM.');
    }
  } finally {
    await shade.shutdown();
  }
}
