/**
 * 1.5.0 Release Test — UTXO v5 Format Support (NEW)
 * 
 * Tests the new v5Format option for UTXO getTransactions():
 *  - v2 format (default, backward compat)
 *  - v5 format (new)
 *  - getTransaction with txTypeVersion 5
 */
import { Translate } from '../dist/index';
import { section, pass, fail, runTest } from './helpers';

const API_KEY = process.env.NOVES_API_KEY!;

export async function testUTXOv5Format() {
  section('NEW: UTXO v5 Format Support');

  const translate = Translate.utxo(API_KEY);

  // Known Bitcoin address with transactions
  const chain = 'btc';
  const address = '3FZbgi29cpjq2GjdwV8eyHuJJnkLtktZc5';

  // 1. Default v2 format (backward compat)
  await runTest('UTXO.getTransactions(v2 default)', async () => {
    const page = await translate.getTransactions(chain, address, { pageSize: 2 });
    const txs = page.getTransactions();
    if (!txs || txs.length === 0) {
      throw new Error('No transactions returned in v2 format');
    }
    // v2 format should have classificationData with sent/received
    const tx = txs[0] as any;
    if (!tx.classificationData) {
      throw new Error('v2 transaction missing classificationData');
    }
    pass('UTXO.getTransactions(v2 default)', `Got ${txs.length} txs, has classificationData`);
  });

  // 2. New v5 format
  await runTest('UTXO.getTransactions(v5Format: true)', async () => {
    const page = await translate.getTransactions(chain, address, { pageSize: 2, v5Format: true });
    const txs = page.getTransactions();
    if (!txs || txs.length === 0) {
      throw new Error('No transactions returned in v5 format');
    }
    const tx = txs[0] as any;
    // v5 format should have top-level transfers/values
    pass('UTXO.getTransactions(v5Format)', `Got ${txs.length} txs, keys: ${Object.keys(tx).join(', ')}`);
  });

  // 3. getTransaction with txTypeVersion = 5
  await runTest('UTXO.getTransaction(txTypeVersion=5)', async () => {
    // First get a transaction hash from the v2 call
    const page = await translate.getTransactions(chain, address, { pageSize: 1 });
    const txs = page.getTransactions();
    if (!txs || txs.length === 0) {
      throw new Error('No transactions to extract hash from');
    }
    const hash = (txs[0] as any).rawTransactionData?.transactionHash 
      || (txs[0] as any).txHash 
      || (txs[0] as any).transactionHash;
    if (!hash) {
      throw new Error(`Could not extract hash from tx: ${JSON.stringify(Object.keys(txs[0]))}`);
    }

    const tx = await translate.getTransaction(chain, hash, 5);
    if (!tx) {
      throw new Error('getTransaction v5 returned null');
    }
    pass('UTXO.getTransaction(v5)', `Tx hash=${hash.substring(0, 16)}..., keys=${Object.keys(tx).join(', ')}`);
  });

  // 4. getTransaction with txTypeVersion = 2 (backward compat)
  await runTest('UTXO.getTransaction(txTypeVersion=2)', async () => {
    const page = await translate.getTransactions(chain, address, { pageSize: 1 });
    const txs = page.getTransactions();
    const hash = (txs[0] as any).rawTransactionData?.transactionHash 
      || (txs[0] as any).txHash;
    if (!hash) throw new Error('No hash');

    const tx = await translate.getTransaction(chain, hash, 2);
    if (!tx) throw new Error('getTransaction v2 returned null');
    pass('UTXO.getTransaction(v2)', `Tx keys=${Object.keys(tx).join(', ')}`);
  });
}
