const loadEnv = require('../config/loadEnv');
const persistentStore = require('../services/persistentStore');
const dbLedgerAdapter = require('../services/dbLedgerAdapter');

loadEnv();

process.env.AISFOR_CHAIN_LEDGER_DUAL_WRITE = '1';
process.env.AISFOR_CHAIN_LEDGER_DB_WRITE = '1';

const state = persistentStore.readState();
const result = dbLedgerAdapter.writeShadowCollections(state);

console.log('[ledger-dual-write-shadow-test] backend:', result.backend);
console.log('[ledger-dual-write-shadow-test] output:', result.shadowDir);
console.log('[ledger-dual-write-shadow-test] documents:', result.totalDocuments);

result.collections.forEach((row) => {
  console.log(`[ledger-dual-write-shadow-test] ${row.collection}: ${row.documents}`);
});

if (!result.totalDocuments) {
  console.error('[ledger-dual-write-shadow-test] no documents were mirrored');
  process.exit(1);
}
