"use strict";
/*
 * @adonisjs/lucid
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
/**
 * The command is meant to migrate the database by execute migrations
 * in `up` direction.
 */
class Status extends standalone_1.BaseCommand {
    constructor() {
        super(...arguments);
        Object.defineProperty(this, "migrator", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Define custom connection
         */
        Object.defineProperty(this, "connection", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
    /**
     * Not a valid connection
     */
    printNotAValidConnection(connection) {
        this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
    }
    /**
     * Colorizes the status string
     */
    colorizeStatus(status) {
        switch (status) {
            case 'pending':
                return this.colors.yellow('pending');
            case 'migrated':
                return this.colors.green('completed');
            case 'corrupt':
                return this.colors.red('corrupt');
        }
    }
    /**
     * Instantiating the migrator instance
     */
    instantiateMigrator() {
        const db = this.application.container.use('Adonis/Lucid/Database');
        const Migrator = this.application.container.resolveBinding('Adonis/Lucid/Migrator');
        this.migrator = new Migrator(db, this.application, {
            direction: 'up',
            connectionName: this.connection,
        });
    }
    /**
     * Render list inside a table
     */
    renderList(list) {
        const table = this.ui.table();
        table.head(['Name', 'Status', 'Batch', 'Message']);
        /**
         * Push a new row to the table
         */
        list.forEach((node) => {
            table.row([
                node.name,
                this.colorizeStatus(node.status),
                node.batch ? String(node.batch) : 'NA',
                node.status === 'corrupt' ? 'The migration file is missing on filesystem' : '',
            ]);
        });
        table.render();
    }
    /**
     * Run as a subcommand. Never close database connections or exit
     * process inside this method
     */
    async runAsSubCommand() {
        const db = this.application.container.use('Adonis/Lucid/Database');
        this.connection = this.connection || db.primaryConnectionName;
        /**
         * Not a valid connection
         */
        if (!db.manager.has(this.connection)) {
            this.printNotAValidConnection(this.connection);
            this.exitCode = 1;
            return;
        }
        this.instantiateMigrator();
        this.renderList(await this.migrator.getList());
    }
    /**
     * Branching out, so that if required we can implement
     * "runAsMain" separately from "runAsSubCommand".
     *
     * For now, they both are the same
     */
    async runAsMain() {
        await this.runAsSubCommand();
    }
    /**
     * Handle command
     */
    async run() {
        if (this.isMain) {
            await this.runAsMain();
        }
        else {
            await this.runAsSubCommand();
        }
    }
    /**
     * Lifecycle method invoked by ace after the "run"
     * method.
     */
    async completed() {
        if (this.migrator && this.isMain) {
            await this.migrator.close();
        }
    }
}
Object.defineProperty(Status, "commandName", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: 'migration:status'
});
Object.defineProperty(Status, "description", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: 'View migrations status'
});
Object.defineProperty(Status, "settings", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: {
        loadApp: true,
    }
});
__decorate([
    standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
    __metadata("design:type", String)
], Status.prototype, "connection", void 0);
exports.default = Status;
