"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const js_yaml_1 = require("js-yaml");
const cli_table3_1 = __importDefault(require("cli-table3"));
const moment = require("moment");
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const util_1 = require("../../commandUtil/util");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const list_1 = require("../../commandUtil/list");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const constants_1 = require("@mablhq-internal/runtime/core/constants");
exports.command = 'list';
exports.describe = 'List recent mabl deployment events';
exports.builder = (0, list_1.getListBuilderOptions)('deployments');
exports.handler = (0, cliUtil_1.failWrapper)(listDeployments);
async function listDeployments(parsed) {
    const output = parsed.output;
    const limit = parsed.limit;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const deployments = await apiClient.getDeploymentEvents(workspaceId, limit);
    printDeployments(deployments, output);
    return deployments.length;
}
function printDeployments(deployments, output) {
    switch (output) {
        case 'json':
            loggingProvider_1.logger.info(JSON.stringify(deployments, null, 2));
            break;
        case 'yaml':
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(deployments));
            break;
        default:
            const table = new cli_table3_1.default({
                head: ['ID', 'Time', 'Application/Env.', 'Pass', 'Fail', 'Total'],
            });
            deployments.forEach((deployment) => {
                table.push([
                    { rowSpan: 2, content: deployment.id, vAlign: 'center' },
                    {
                        rowSpan: 2,
                        content: moment
                            .utc(deployment.received_time)
                            .format(constants_1.ListTimeFormat),
                        vAlign: 'center',
                    },
                    deployment.application_id || 'none',
                    {
                        rowSpan: 2,
                        content: deployment.run_result?.journey_execution_metrics?.passed,
                        vAlign: 'center',
                        hAlign: 'center',
                    },
                    {
                        rowSpan: 2,
                        content: deployment.run_result?.journey_execution_metrics?.failed,
                        vAlign: 'center',
                        hAlign: 'center',
                    },
                    {
                        rowSpan: 2,
                        content: deployment.run_result?.journey_execution_metrics?.total,
                        vAlign: 'center',
                        hAlign: 'center',
                    },
                ], [deployment.environment_id || 'none']);
            });
            loggingProvider_1.logger.info(table.toString());
            break;
    }
}
