"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const env_js_1 = require("../config/env.js");
const connection_js_1 = require("../infrastructure/database/connection.js");
const SweepVaultModel_js_1 = require("../infrastructure/database/models/SweepVaultModel.js");
const Crypto_js_1 = require("../infrastructure/payments/Crypto.js");
const MnemonicWallet_js_1 = require("../infrastructure/payments/MnemonicWallet.js");
const Logger_js_1 = require("../shared/logger/Logger.js");
const config = (0, env_js_1.loadEnv)();
const logger = new Logger_js_1.Logger(config.logLevel);
async function main() {
    if (!config.mongoUri) {
        logger.error("MONGO_URI is not configured; cannot create sweep vault.");
        return 1;
    }
    if (!config.paymentEncryptionSecret) {
        logger.error("PAYMENT_ENCRYPTION_SECRET is not configured; cannot encrypt vault seed phrase.");
        return 1;
    }
    const connected = await (0, connection_js_1.connectDatabase)(config.mongoUri, logger);
    if (!connected) {
        logger.error("Failed to connect to the database.");
        return 1;
    }
    try {
        const wallet = (0, MnemonicWallet_js_1.generateVaultWallet)();
        const cryptoBox = new Crypto_js_1.CryptoBox(config.paymentEncryptionSecret);
        const vault = await SweepVaultModel_js_1.SweepVaultModel.create({
            evmAddress: wallet.evmAddress,
            tronAddress: wallet.tronAddress,
            encryptedSeedPhrase: cryptoBox.encrypt(wallet.seedPhrase),
            keyReference: "local:aes-256-gcm",
            isActive: true
        });
        logger.info("Sweep vault created successfully.", {
            id: vault._id.toString(),
            evmAddress: wallet.evmAddress,
            tronAddress: wallet.tronAddress
        });
        // Print addresses to stdout for scripts / operators.
        // eslint-disable-next-line no-console
        console.log(`Vault ID: ${vault._id.toString()}`);
        // eslint-disable-next-line no-console
        console.log(`EVM Address: ${wallet.evmAddress}`);
        // eslint-disable-next-line no-console
        console.log(`Tron Address: ${wallet.tronAddress}`);
        return 0;
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        logger.error("Failed to create sweep vault.", { error: message });
        return 1;
    }
    finally {
        await (0, connection_js_1.disconnectDatabase)();
    }
}
main()
    .then((exitCode) => {
    process.exitCode = exitCode;
})
    .catch((error) => {
    logger.error("Failed to create sweep vault.", { error: error instanceof Error ? error.message : String(error) });
    process.exitCode = 1;
});
