/**
 * Vaultfire SDK smoke test — TRUTH-ONLY edition.
 *
 * Every method called below corresponds to a real on-chain function copied
 * verbatim from the canonical Solidity sources at gitlawb. No invented APIs.
 */

const { VaultfireSDK } = require('./dist/index.js');

const TEST_ADDRESS = '0xA054f831B562e729F8D268291EBde1B2EDcFb84F';

(async () => {
  const sdk = new VaultfireSDK({ chain: 'base' });

  console.log('=== 1. Trust profile (live API) ===');
  const profile = await sdk.getAgentTrustProfile(TEST_ADDRESS);
  console.log(JSON.stringify(profile, null, 2));

  console.log('\n=== 2. Partnership bond IDs for the address (on-chain read) ===');
  const ids = await sdk.bonds.listPartnershipBonds(TEST_ADDRESS);
  console.log('partnership bond IDs:', ids);

  if (ids.length > 0) {
    const firstId = ids[0];

    console.log(`\n=== 3. getPartnershipBond(${firstId}) — full struct ===`);
    const bond = await sdk.bonds.getPartnershipBond(firstId);
    console.log(JSON.stringify(bond, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));

    console.log(`\n=== 4. getPartnershipBondValue(${firstId}) — value + appreciation ===`);
    const value = await sdk.bonds.getPartnershipBondValue(firstId);
    console.log(JSON.stringify(value, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
  }

  await new Promise(r => setTimeout(r, 4000)); // pace public RPC
  console.log('\n=== 5. Insurance pool stats (on-chain read) ===');
  const pool = await sdk.bonds.getInsurancePoolStats();
  console.log(JSON.stringify(pool, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));

  await new Promise(r => setTimeout(r, 800));
  console.log('\n=== 6. Is the address insured? ===');
  const insured = await sdk.bonds.isInsured(TEST_ADDRESS);
  console.log('isInsured:', insured);

  console.log('\n=== 7. isTrustedEnough(>=30) convenience ===');
  const ok = await sdk.trust.isTrustedEnough(TEST_ADDRESS, 30);
  console.log('Score >= 30?', ok);

  console.log('\n=== 8. Tx-builders — unsigned, no signer needed ===');
  const accBondTx = sdk.bonds.buildCreateAccountabilityBondTx({
    companyName: 'TestCo',
    quarterlyRevenue: '100000',
    stakeETH: '0.001',
  });
  console.log('buildCreateAccountabilityBondTx:', { ...accBondTx, dataLen: accBondTx.data.length });

  const reqTx = sdk.bonds.buildRequestAccountabilityDistributionTx(1n);
  console.log('buildRequestAccountabilityDistributionTx:', { ...reqTx, dataLen: reqTx.data.length });

  const insTx = sdk.bonds.buildContributeInsuranceTx('0.0001');
  console.log('buildContributeInsuranceTx:', { ...insTx, dataLen: insTx.data.length });

  const taskTx = sdk.tasks.buildCreateTaskTx({
    description: 'Fix bug #42',
    rewardUSDC: '5.00',
    deadline: Math.floor(Date.now() / 1000) + 86400 * 7,
  });
  console.log('buildCreateTaskTx:', { ...taskTx, dataLen: taskTx.data.length });

  const approveTx = sdk.tasks.buildApproveWorkTx(1n);
  console.log('buildApproveWorkTx:', { ...approveTx, dataLen: approveTx.data.length });

  const usdcApprove = sdk.tasks.buildApproveUSDCTx('5.00');
  console.log('buildApproveUSDCTx (USDC):', { ...usdcApprove, dataLen: usdcApprove.data.length });

  console.log('\n✅ All real-method smoke checks passed.');
})().catch(e => { console.error('FAIL:', e); process.exit(1); });
