const assert = require('assert');
const {
  getRuntimeConfigSnapshot,
  loadMazexConfig,
} = require('../src/lib/config');
const {
  buildFreshBase1State,
  getBase1FeeSchedule,
  getBase1RuntimeConfig,
} = require('../src/lib/base1_state');
const { ADDRESS_PREFIX, createWallet } = require('../src/lib/wallet');

const loaded = loadMazexConfig({ reload: true });
const snapshot = getRuntimeConfigSnapshot();
const state = buildFreshBase1State();
const fees = getBase1FeeSchedule();
const wallet = createWallet();
const stateConfig = getBase1RuntimeConfig();

assert.strictEqual(snapshot.chain.chain_id, 'mazexchain-base1-local');
assert.strictEqual(state.chainId, snapshot.chain.chain_id);
assert.strictEqual(state.genesisCreatedAt, snapshot.genesis.genesis_time_utc);
assert.strictEqual(state.currentLayer, snapshot.chain.initial_layer);
assert.strictEqual(state.currentStep, snapshot.chain.initial_step);
assert.strictEqual(fees.sample_fee, snapshot.fees.sample_fee);
assert.strictEqual(fees.submit_fee, snapshot.fees.submit_fee_min);
assert.strictEqual(fees.transfer_fee, snapshot.fees.transfer_fee);
assert.strictEqual(ADDRESS_PREFIX, snapshot.wallet.address_prefix);
assert(wallet.address.startsWith(snapshot.wallet.address_prefix), 'wallet address prefix is not loaded from wallet config');
assert.deepStrictEqual(stateConfig.chain, snapshot.chain);
assert(Array.isArray(loaded.initial_accounts.accounts), 'initial accounts config must load');
assert(Array.isArray(loaded.initial_fragments.registry.active_fragments), 'initial fragments config must load');

console.log({
  status: 'pass',
  chain_id: state.chainId,
  genesis_time_utc: state.genesisCreatedAt,
  initial_position: `${state.currentLayer}:${state.currentStep}`,
  fees: {
    sample: fees.sample_fee,
    submit_min: fees.submit_fee,
    transfer: fees.transfer_fee,
  },
  wallet_prefix: ADDRESS_PREFIX,
  public_config_sections: Object.keys(snapshot).length,
});
