/**
 * 1.5.0 Release Test — Move Translate API (NEW)
 * 
 * Tests the brand-new Move ecosystem Translate API:
 *  - getChains()
 *  - getTransactionTypes()
 *  - getTransaction() (using a real hash from getTransactions)
 *  - getTransactions() with pagination
 */
import { Translate } from '../dist/index';
import { section, pass, fail, runTest } from './helpers';

const API_KEY = process.env.NOVES_API_KEY!;

export async function testMoveTranslate() {
  section('NEW: Move Translate API');

  const translate = Translate.move(API_KEY);
  const chain = 'sui';

  // 1. getChains
  await runTest('Move.getChains()', async () => {
    const chains = await translate.getChains();
    if (!chains || (Array.isArray(chains) && chains.length === 0)) {
      throw new Error('getChains() returned empty result');
    }
    pass('Move.getChains()', `Received chains data: ${JSON.stringify(chains).substring(0, 120)}...`);
  });

  // 2. getTransactionTypes
  await runTest('Move.getTransactionTypes()', async () => {
    const types = await translate.getTransactionTypes();
    if (!types) {
      throw new Error('getTransactionTypes() returned empty');
    }
    const typeStr = JSON.stringify(types).substring(0, 200);
    pass('Move.getTransactionTypes()', `Received types: ${typeStr}...`);
  });

  // 3. getTransactions — find a real address with transactions
  //    We'll use a well-known Sui address
  let realTxId: string | null = null;

  await runTest('Move.getTransactions(sui)', async () => {
    // Try a few known active Sui addresses
    const addresses = [
      '0xd77a6613e47027457e0a0466c01e1f6c2a241e73ccc383fc47b0388f542e4c44',
      '0x7d20dcdb2bca4f508ea9613994683eb4e76e9c4ed371169677c1be02aaf0b58e',
      '0x2f1c55f14e7415ab6a26eb3f3ad58f3af81e9a1e7a3e02db28e1b4b2da25c6b0'
    ];

    let succeeded = false;
    for (const address of addresses) {
      try {
        const page = await translate.getTransactions(chain, address, { pageSize: 3 });
        if (!page) continue;
        const txs = page.getTransactions();
        if (txs && txs.length > 0) {
          // Extract the tx id/hash from the first transaction
          const tx = txs[0] as any;
          realTxId = tx.rawTransactionData?.transactionHash 
            || tx.rawTransactionData?.txHash
            || tx.rawTransactionData?.digest
            || tx.txHash 
            || tx.transactionHash
            || tx.digest
            || tx.hash;
          // Debug: log keys if hash not found
          if (!realTxId) {
            console.log(`    DEBUG Move tx keys: ${JSON.stringify(Object.keys(tx))}`);
            if (tx.rawTransactionData) {
              console.log(`    DEBUG Move rawTxData keys: ${JSON.stringify(Object.keys(tx.rawTransactionData))}`);
            }
          }
          pass('Move.getTransactions()', `Got ${txs.length} txs from ${address.substring(0, 16)}..., hasNext=${page.hasNext()}`);
          
          // Test next page if available
          if (page.hasNext()) {
            const page2 = await page.next() as any;
            if (page2 && typeof page2.getTransactions === 'function') {
              const txs2 = page2.getTransactions();
              pass('Move.getTransactions().next()', `Page 2 has ${txs2.length} transactions`);
            }
          }
          succeeded = true;
          break;
        }
      } catch (e) {
        // Try next address
        continue;
      }
    }
    if (!succeeded) {
      throw new Error('Could not find Move transactions for any test address');
    }
  });

  // 4. getTransaction — use the real hash we found
  if (realTxId) {
    await runTest('Move.getTransaction(sui, realTxId)', async () => {
      const tx = await translate.getTransaction(chain, realTxId!);
      if (!tx) {
        throw new Error('getTransaction() returned null/undefined');
      }
      if (!tx.classificationData) {
        throw new Error('Transaction missing classificationData');
      }
      pass('Move.getTransaction()', `type=${tx.classificationData.type}, desc="${tx.classificationData.description?.substring(0, 80)}"`);
    });
  } else {
    fail('Move.getTransaction()', 'Skipped — no real tx hash available from getTransactions');
  }
}
