import { entity } from '../output/style.js';
import { table } from '../output/table.js';
import { getApplicationVersionLogs } from '../srv/get-application-version-logs.js';
import { createApplicationVersionCommand } from './common/application-version-command-factory.js';

const command = createApplicationVersionCommand({
    name: 'logs',
    desc: 'Get logs of application version',
    usage: 'afctl logs APPLICATION [VERSION]',
    serviceCall: getApplicationVersionLogs,
    formatOutput: (logs) => [
        entity('Logs\n'),
        ...(logs?.value?.map?.(item => table({
            type: 'columns',
            headers: [
                { name: 'timestamp', title: 'Time' },
                { name: 'level', title: 'Level' },
                { name: 'correlationId', title: 'Correlation ID' },
                { name: 'msg', title: 'Message' }
            ],
            data: [{
                timestamp: item.timestamp ? (new Date(item.timestamp)).toISOString() : '-',
                level: item.level?.toUpperCase?.() ?? '-',
                correlationId: item.correlationId ?? '-',
                msg: (item.msg ? item.msg : '-') + '\n'
            }]
        })) ?? [])
    ]
});

export const getMetadata = command.getMetadata;
export const getCompletion = command.getCompletion;
export const run = command.run;
