"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const _cliter_1 = require("../@cliter");
const back_1 = require("../@cliter/functions/back");
const common_1 = require("../@cliter/functions/common");
class Generate extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(Generate);
        if (args.firstArg === _cliter_1.Scope.BACK &&
            args.secondArg === _cliter_1.ScopeElement.MODULE) {
            const flagName = (0, common_1.getBoundedContextModuleFromFlag)(this, flags.name);
            const { boundedContextName, moduleName, moduleNames, hasOAuth, hasTenant } = await _cliter_1.Prompter.promptForGenerateModule(flagName.boundedContextName, flagName.moduleName);
            const properties = new _cliter_1.Properties();
            properties.moduleName = moduleName;
            // add id property for model
            properties.add(new _cliter_1.Property({
                name: 'id',
                type: _cliter_1.SqlType.ID,
                primaryKey: true,
                length: 36,
                nullable: false,
            }));
            // define state for generate command
            const generateCommandState = {
                command: this,
                flags,
                lockFiles: [],
                schema: {
                    boundedContextName,
                    moduleName,
                    moduleNames,
                    aggregateName: boundedContextName.toPascalCase() + moduleName.toPascalCase(),
                    hasOAuth,
                    hasTenant,
                    hasAuditing: false,
                    properties,
                    additionalApis: new _cliter_1.AdditionalApis(),
                },
            };
            // add properties defined by user
            // eslint-disable-next-line no-await-in-loop
            while ((await _cliter_1.Prompter.promptForGenerateAggregate()).hasValueObject) {
                // eslint-disable-next-line no-await-in-loop
                properties.add(await _cliter_1.Prompter.promptDefineAggregateProperty(generateCommandState));
                _cliter_1.Prompter.printValueObjectsTable(this, properties);
            }
            // add time stamp properties for model
            properties.add(new _cliter_1.Property({ name: 'createdAt', type: _cliter_1.SqlType.TIMESTAMP, nullable: true }));
            properties.add(new _cliter_1.Property({ name: 'updatedAt', type: _cliter_1.SqlType.TIMESTAMP, nullable: true }));
            properties.add(new _cliter_1.Property({ name: 'deletedAt', type: _cliter_1.SqlType.TIMESTAMP, nullable: true }));
            // generate module files
            _cliter_1.BackHandler.generateModule(generateCommandState, { hasGenerateTestingFiles: true });
            if (args.firstArg === _cliter_1.Scope.BACK && !flags.noGraphQLTypes) {
                await (0, back_1.generateGraphqlTypes)(generateCommandState);
            }
        }
    }
}
exports.default = Generate;
Generate.description = 'Generate aurora item';
Generate.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    force: core_1.Flags.boolean({
        char: 'f',
        description: 'Overwrite existing files.',
    }),
    name: core_1.Flags.string({
        char: 'n',
        required: true,
        description: 'Name of element to generate.',
    }),
    noGraphQLTypes: core_1.Flags.boolean({
        char: 'g',
        description: 'Avoid generating graphql types.',
    }),
    verbose: core_1.Flags.boolean({
        char: 'v',
        description: 'Reports on screen all the steps followed by the command.',
    }),
};
Generate.args = {
    firstArg: core_1.Args.string({
        name: 'scope',
        required: true,
        description: 'Scope where our command will act.',
        options: [
            'back',
            'front',
        ],
    }),
    secondArg: core_1.Args.string({
        name: 'element',
        required: true,
        description: 'Type of element to generate.',
        options: [
            'api',
            'bounded-context',
            'module',
        ],
    }),
};
Generate.examples = [
    '$ aurora generate back module -n=my-bounded-context/my-module',
    '$ aurora --help',
];
