require('../config/loadEnv')();

const { TronWeb } = require('tronweb');
const { MULTI_CHAIN_WALLET_CONFIG } = require('../config/multiChainWalletConfig');
const walletService = require('../services/multiChainWalletService');

async function main() {
  const evmFixture = '0x000000000000000000000000000000000000dEaD';
  const tronAddress = walletService.deriveTronAddress(evmFixture);
  const amount = walletService.validateDecimalAmount('1.000001', 6, '0.000001', 'SELF_TEST_AMOUNT');
  let lockedError = null;
  try {
    await walletService.sendTrx({ to: tronAddress, amount: '1' });
  } catch (error) {
    lockedError = error;
  }
  const checks = {
    tronAddressDerivation: TronWeb.isAddress(tronAddress),
    decimalValidation: amount.raw === 1000001n,
    runtimeTransferLockConfigured: MULTI_CHAIN_WALLET_CONFIG.transfersEnabled === false,
    transferPathFailsClosedBeforeSigning: lockedError?.code === 'MULTICHAIN_TRANSFERS_DISABLED'
  };
  const ok = Object.values(checks).every(Boolean);
  console.log(JSON.stringify({
    ok,
    schemaVersion: 'plais-multichain-dependency-compatibility/v1',
    networkRequestsPerformed: false,
    productionWritesPerformed: false,
    checks
  }, null, 2));
  if (!ok) process.exitCode = 1;
}

main().catch((error) => {
  console.error(JSON.stringify({ ok: false, error: error.code || error.message }, null, 2));
  process.exit(1);
});
