/**
 * Run All SDK Integration Tests
 *
 * Executes each test module sequentially via subprocess.
 * Each test is independent — failures in one don't block others.
 *
 * Usage:
 *   source .env.local && npx tsx scripts/run-all.ts
 */

import { execFileSync } from 'child_process';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const tests = [
  { name: 'Wallet & Balance', file: 'test-wallet.ts' },
  { name: 'Send', file: 'test-send.ts' },
  { name: 'NAVI Protocol', file: 'test-navi.ts' },
  { name: 'Earn', file: 'test-earn.ts' },
  { name: 'Claim Rewards', file: 'test-claim-rewards.ts' },
  { name: 'MPP Pay', file: 'test-pay.ts' },
  { name: 'Misc', file: 'test-misc.ts' },
];

console.log('╔══════════════════════════════════════════╗');
console.log('║   t2000 SDK — Full Integration Suite     ║');
console.log('╚══════════════════════════════════════════╝');
console.log(`\n   Tests: ${tests.length}`);
console.log(`   Mode:  Standard\n`);

let totalPassed = 0;
let totalFailed = 0;
const results: { name: string; status: 'passed' | 'failed'; time: number }[] = [];

for (const test of tests) {
  const scriptPath = resolve(__dirname, test.file);
  const start = Date.now();

  console.log(`\n${'═'.repeat(50)}`);
  console.log(`  Running: ${test.name}`);
  console.log(`${'═'.repeat(50)}`);

  try {
    const execArgs = ['tsx', scriptPath];
    execFileSync('npx', execArgs, { stdio: 'inherit' });
    const elapsed = Date.now() - start;
    results.push({ name: test.name, status: 'passed', time: elapsed });
    totalPassed++;
  } catch {
    const elapsed = Date.now() - start;
    results.push({ name: test.name, status: 'failed', time: elapsed });
    totalFailed++;
  }
}

console.log('\n╔══════════════════════════════════════════╗');
console.log('║   Summary                               ║');
console.log('╚══════════════════════════════════════════╝\n');

for (const r of results) {
  const icon = r.status === 'passed' ? '✓' : '✗';
  const time = `${(r.time / 1000).toFixed(1)}s`;
  console.log(`   ${icon} ${r.name.padEnd(25)} ${time}`);
}

console.log(`\n   ${totalPassed} passed, ${totalFailed} failed\n`);

process.exit(totalFailed > 0 ? 1 : 0);
