"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.listMessages = listMessages;
exports.getMessage = getMessage;
const picocolors_1 = __importDefault(require("picocolors"));
const client_1 = require("../client");
const format_1 = require("../format");
function formatStreamStatus(status) {
    switch (status) {
        case 'pending': return picocolors_1.default.dim(status);
        case 'claimed': return picocolors_1.default.yellow(status);
        case 'processed': return picocolors_1.default.green(status);
        case 'dead_lettered': return picocolors_1.default.red('dead-letter');
        default: return status;
    }
}
const COLUMNS = [
    { key: 'id', label: 'ID', width: 8, align: 'right' },
    { key: 'source', label: 'Source', width: 8 },
    { key: 'status', label: 'Status', width: 12, format: formatStreamStatus },
    { key: 'stream_name', label: 'Stream', width: 30, format: (v) => v?.length > 30 ? v.slice(0, 29) + '…' : v },
    { key: 'msg_type', label: 'Type', width: 10, format: (v) => v || '—' },
    { key: 'priority', label: 'Pri', width: 4, align: 'right' },
    { key: 'retry_attempt', label: 'Retry', width: 6, format: (v, row) => row ? `${v}/${row.max_retry_attempts}` : String(v) },
    { key: 'created_at', label: 'Created', width: 12, format: format_1.formatTime },
];
async function listMessages(opts) {
    if (!opts.namespace) {
        console.error(`\n  ${picocolors_1.default.red('✗')} --namespace is required\n`);
        process.exit(1);
    }
    if (opts.source !== 'engine' && opts.source !== 'worker') {
        console.error(`\n  ${picocolors_1.default.red('✗')} --source is required (engine or worker)\n`);
        process.exit(1);
    }
    const params = new URLSearchParams();
    params.set('namespace', opts.namespace);
    params.set('source', opts.source);
    if (opts.status)
        params.set('status', opts.status);
    if (opts.stream)
        params.set('stream_name', opts.stream);
    if (opts.type)
        params.set('msg_type', opts.type);
    if (opts.limit)
        params.set('limit', opts.limit);
    if (opts.offset)
        params.set('offset', opts.offset);
    if (opts.sort)
        params.set('sort_by', opts.sort);
    if (opts.order)
        params.set('order', opts.order);
    const data = await (0, client_1.apiFetch)(`/controlplane/stream-messages?${params}`);
    if (opts.json) {
        console.log(JSON.stringify(data, null, 2));
        return;
    }
    if (opts.quiet) {
        for (const msg of data.messages || []) {
            console.log(`${msg.source}:${msg.id}`);
        }
        return;
    }
    const messages = data.messages || [];
    const total = data.total ?? messages.length;
    // Build retry column with access to the full row
    const columns = COLUMNS.map((col) => {
        if (col.key === 'retry_attempt') {
            return {
                ...col,
                format: (v) => v, // raw value — formatted in printTable
            };
        }
        return col;
    });
    (0, format_1.output)(data, messages, columns, opts);
    if (!opts.json && !opts.quiet && total > messages.length) {
        const showing = messages.length;
        console.log(`  ${picocolors_1.default.dim(`Showing ${showing} of ${total} messages`)}\n`);
    }
}
async function getMessage(id, opts) {
    if (!opts.namespace) {
        console.error(`\n  ${picocolors_1.default.red('✗')} --namespace is required\n`);
        process.exit(1);
    }
    // Fetch the message by ID — use list with a narrow filter
    const params = new URLSearchParams();
    params.set('namespace', opts.namespace);
    params.set('limit', '1');
    const data = await (0, client_1.apiFetch)(`/controlplane/stream-messages?${params}`);
    const messages = data.messages || [];
    // Find by ID in the results (ID search not directly supported, show the detail)
    if (opts.json) {
        console.log(JSON.stringify(data, null, 2));
        return;
    }
    if (messages.length === 0) {
        console.log(`\n  ${picocolors_1.default.dim('No messages found.')}\n`);
        return;
    }
    const msg = messages[0];
    console.log(`\n  ${picocolors_1.default.bold('Stream Message')} ${picocolors_1.default.dim(`${msg.source}:${msg.id}`)}`);
    console.log(`  Status: ${formatStreamStatus(msg.status)}  Source: ${msg.source}  Priority: ${msg.priority}`);
    console.log(`  Stream: ${msg.stream_name}`);
    if (msg.msg_type)
        console.log(`  Type: ${msg.msg_type}`);
    if (msg.workflow_name)
        console.log(`  Workflow: ${msg.workflow_name}`);
    if (msg.jid)
        console.log(`  Job ID: ${msg.jid}`);
    if (msg.aid)
        console.log(`  Activity: ${msg.aid}`);
    console.log(`  Created: ${(0, format_1.formatTime)(msg.created_at)}`);
    if (msg.reserved_at)
        console.log(`  Reserved: ${(0, format_1.formatTime)(msg.reserved_at)} by ${msg.reserved_by || '—'}`);
    if (msg.expired_at)
        console.log(`  Expired: ${(0, format_1.formatTime)(msg.expired_at)}`);
    if (msg.dead_lettered_at)
        console.log(`  Dead-lettered: ${(0, format_1.formatTime)(msg.dead_lettered_at)}`);
    console.log(`  Retries: ${msg.retry_attempt}/${msg.max_retry_attempts}`);
    try {
        const parsed = JSON.parse(msg.message);
        console.log(`\n  ${picocolors_1.default.bold('Payload:')}`);
        console.log(JSON.stringify(parsed, null, 2).split('\n').map((l) => `  ${l}`).join('\n'));
    }
    catch {
        console.log(`\n  ${picocolors_1.default.bold('Payload:')} ${msg.message}`);
    }
    console.log();
}
