"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
require("reflect-metadata");
const data_source_1 = require("../infrastructure/persistence/data-source");
const pino_logger_1 = require("../infrastructure/logging/pino-logger");
const wallet_entity_1 = require("../infrastructure/persistence/entities/wallet.entity");
const network_entity_1 = require("../infrastructure/persistence/entities/network.entity");
const address_entity_1 = require("../infrastructure/persistence/entities/address.entity");
const address_mapper_1 = require("../infrastructure/persistence/repositories/mappers/address.mapper");
const DRY_RUN = process.env.DRY_RUN === 'true';
const run = async () => {
    await (0, data_source_1.initializeDatabase)();
    const logger = new pino_logger_1.PinoLogger();
    const walletRepo = data_source_1.AppDataSource.getRepository(wallet_entity_1.WalletEntity);
    const networkRepo = data_source_1.AppDataSource.getRepository(network_entity_1.NetworkEntity);
    const addressRepo = data_source_1.AppDataSource.getRepository(address_entity_1.AddressEntity);
    const [walletEntities, networkEntities, addressEntities] = await Promise.all([
        walletRepo.find(),
        networkRepo.find({ where: { status: 'active' }, order: { createdAt: 'ASC' } }),
        addressRepo.find(),
    ]);
    const activeNetworks = networkEntities;
    let updated = 0;
    let skipped = 0;
    for (const wallet of walletEntities) {
        const walletAddresses = addressEntities.filter((a) => a.walletId === wallet.id && !a.networkId);
        for (const network of activeNetworks) {
            const matchIndex = walletAddresses.findIndex((a) => a.type === network.addressType);
            if (matchIndex === -1) {
                continue;
            }
            const entity = walletAddresses.splice(matchIndex, 1)[0];
            const domain = address_mapper_1.addressMapper.toDomain(entity);
            const updatedDomain = domain.setNetworkId(network.id);
            const updatedEntity = address_mapper_1.addressMapper.toEntity(updatedDomain);
            if (DRY_RUN) {
                logger.info('[DRY RUN] Would backfill address network', {
                    addressId: entity.id,
                    walletId: wallet.id,
                    networkId: network.id,
                    networkName: network.name,
                });
            }
            else {
                await addressRepo.save(updatedEntity);
                logger.info('Backfilled address network', {
                    addressId: entity.id,
                    walletId: wallet.id,
                    networkId: network.id,
                    networkName: network.name,
                });
            }
            updated++;
        }
        skipped += walletAddresses.length;
    }
    logger.info('Address network backfill complete', {
        dryRun: DRY_RUN,
        wallets: walletEntities.length,
        networks: activeNetworks.length,
        updated,
        skipped,
    });
    process.exit(updated > 0 || skipped === 0 ? 0 : 0);
};
run().catch((error) => {
    // eslint-disable-next-line no-console
    console.error('Address network backfill failed', error);
    process.exit(1);
});
//# sourceMappingURL=backfill-address-networks.js.map