"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var TestDataCommand_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestDataCommand = void 0;
const common_1 = require("@nestjs/common");
const nest_commander_1 = require("nest-commander");
const module_test_data_service_1 = require("../seeders/module-test-data.service");
let TestDataCommand = TestDataCommand_1 = class TestDataCommand extends nest_commander_1.CommandRunner {
    constructor(testDataService) {
        super();
        this.testDataService = testDataService;
        this.logger = new common_1.Logger(TestDataCommand_1.name);
    }
    async run(_passedParam, options) {
        try {
            const load = Boolean(options?.load);
            const setup = Boolean(options?.setup);
            const teardown = Boolean(options?.teardown);
            const unlink = Boolean(options?.unlink);
            const selectedModes = [load, setup, teardown, unlink].filter(Boolean).length;
            if (selectedModes > 1) {
                this.logger.error('Please specify only one of --load, --setup, --teardown, or --unlink.');
                console.log('Please specify only one of --load, --setup, --teardown, or --unlink.');
                return;
            }
            if (!load && !setup && !teardown && !unlink) {
                this.logger.error('Please specify one of --load, --setup, --teardown, or --unlink.');
                console.log('Please specify one of --load, --setup, --teardown, or --unlink.');
                return;
            }
            if (teardown) {
                this.logger.log('Deleting test datasource environment and databases.');
                console.log('Deleting test datasource environment and databases.');
                await this.testDataService.deleteTestDatasources();
                return;
            }
            if (setup) {
                this.logger.log('Creating test datasource environment file and manifest.');
                console.log('Creating test datasource environment file and manifest.');
                await this.testDataService.createTestDatasources();
                return;
            }
            if (load) {
                const modulesToTest = options?.modulesToTest ? options.modulesToTest.split(',').map((m) => m.trim()).filter(Boolean) : null;
                if (modulesToTest?.length) {
                    this.logger.log(`Test data setup for modules: ${modulesToTest.join(', ')}`);
                    console.log(`Test data setup for modules: ${modulesToTest.join(', ')}`);
                }
                else {
                    this.logger.log('Test data setup for all modules.');
                    console.log('Test data setup for all modules.');
                }
                await this.testDataService.setupTestData(modulesToTest ?? undefined);
                return;
            }
            if (unlink) {
                const modulesToTest = options?.modulesToTest ? options.modulesToTest.split(',').map((m) => m.trim()).filter(Boolean) : null;
                if (modulesToTest?.length) {
                    this.logger.log(`Test data unlink for modules: ${modulesToTest.join(', ')}`);
                    console.log(`Test data unlink for modules: ${modulesToTest.join(', ')}`);
                }
                else {
                    this.logger.log('Test data unlink for all modules.');
                    console.log('Test data unlink for all modules.');
                }
                await this.testDataService.removeTestData(modulesToTest ?? undefined);
                return;
            }
        }
        catch (err) {
            const message = err?.message ?? String(err);
            this.logger.error(message);
            console.error(`Test data command failed: ${message}`);
            if (err?.stack) {
                console.error(err.stack);
            }
            throw err;
        }
    }
    parseLoad() {
        return true;
    }
    parseSetup() {
        return true;
    }
    parseTeardown() {
        return true;
    }
    parseModulesToTest(val) {
        return val;
    }
    parseUnlink() {
        return true;
    }
};
exports.TestDataCommand = TestDataCommand;
__decorate([
    (0, nest_commander_1.Option)({
        flags: '--load',
        description: 'Seed test data from testing.data sections',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Boolean)
], TestDataCommand.prototype, "parseLoad", null);
__decorate([
    (0, nest_commander_1.Option)({
        flags: '--setup',
        description: 'Create a new .env.<dbRunName> and test datasource manifest',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Boolean)
], TestDataCommand.prototype, "parseSetup", null);
__decorate([
    (0, nest_commander_1.Option)({
        flags: '--teardown',
        description: 'Delete test datasource env/manifest and drop test databases',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Boolean)
], TestDataCommand.prototype, "parseTeardown", null);
__decorate([
    (0, nest_commander_1.Option)({
        flags: '--modules-to-test [module names]',
        description: 'Comma-separated list of module names to test (defaults to all modules).',
        required: false,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", String)
], TestDataCommand.prototype, "parseModulesToTest", null);
__decorate([
    (0, nest_commander_1.Option)({
        flags: '--unlink',
        description: 'Delete test data from testing.data sections in reverse order',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Boolean)
], TestDataCommand.prototype, "parseUnlink", null);
exports.TestDataCommand = TestDataCommand = TestDataCommand_1 = __decorate([
    (0, nest_commander_1.SubCommand)({
        name: 'data',
        description: 'Seed test data from testing.data sections',
    }),
    __metadata("design:paramtypes", [module_test_data_service_1.ModuleTestDataService])
], TestDataCommand);
//# sourceMappingURL=test-data.command.js.map