// tools/worker-status-handler.ts — Handler for worker_status tool (SPEC-078 AC-11)
import { getWorkerEngine } from '../engine/workers/index.js';
import { t } from '../i18n/index.js';
// ---------------------------------------------------------------------------
// Formatting helpers
// ---------------------------------------------------------------------------
function formatWorkerStatus(ws) {
    const status = ws.enabled ? 'enabled' : 'disabled';
    const running = ws.running ? ' (running)' : '';
    const lastRun = ws.lastRun
        ? `last run: ${ws.lastRun.status} (${String(ws.lastRun.durationMs)}ms, ${String(ws.lastRun.findings.length)} findings)`
        : 'never run';
    const failures = ws.consecutiveFailures > 0 ? ` | failures: ${String(ws.consecutiveFailures)}` : '';
    const cooldown = ` | cooldown: ${String(ws.effectiveCooldownMs)}ms`;
    return `  ${ws.name}: ${status}${running} | ${lastRun}${failures}${cooldown}`;
}
function formatDetailedStatus(ws) {
    const lines = [];
    lines.push(`Worker: ${ws.name}`);
    lines.push(`Status: ${ws.enabled ? 'enabled' : 'disabled'}${ws.running ? ' (running)' : ''}`);
    lines.push(`Consecutive failures: ${String(ws.consecutiveFailures)}`);
    lines.push(`Effective cooldown: ${String(ws.effectiveCooldownMs)}ms`);
    if (ws.lastRun) {
        lines.push('');
        lines.push('Last Run:');
        lines.push(`  Status: ${ws.lastRun.status}`);
        lines.push(`  Started: ${ws.lastRun.startedAt}`);
        lines.push(`  Duration: ${String(ws.lastRun.durationMs)}ms`);
        lines.push(`  Files analyzed: ${String(ws.lastRun.analyzedFiles)}`);
        lines.push(`  Findings: ${String(ws.lastRun.findings.length)}`);
        if (ws.lastRun.error) {
            lines.push(`  Error: ${ws.lastRun.error}`);
        }
        if (ws.lastRun.findings.length > 0) {
            lines.push('');
            lines.push('Findings:');
            for (const f of ws.lastRun.findings.slice(0, 20)) {
                const loc = f.line ? `:${String(f.line)}` : '';
                lines.push(`  [${f.severity}] ${f.file}${loc}: ${f.message}`);
                if (f.suggestion) {
                    lines.push(`    -> ${f.suggestion}`);
                }
            }
            if (ws.lastRun.findings.length > 20) {
                lines.push(`  ... and ${String(ws.lastRun.findings.length - 20)} more`);
            }
        }
    }
    else {
        lines.push('');
        lines.push('No previous runs recorded.');
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export function handleWorkerStatus(args) {
    const engine = getWorkerEngine();
    if (args.workerName) {
        const ws = engine.getStatus(args.workerName);
        if (!ws) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `Worker "${args.workerName}" not found. Use worker_status without workerName to see all available workers.`,
                    },
                ],
                isError: true,
            };
        }
        return {
            content: [{ type: 'text', text: formatDetailedStatus(ws) }],
        };
    }
    const engineStatus = engine.getStatus();
    const engineLabel = engineStatus.active
        ? t('tools.response.active')
        : t('tools.response.stopped');
    const lines = [];
    lines.push(t('tools.response.workerTitle'));
    lines.push('');
    lines.push(`Worker Engine: ${engineLabel}`);
    lines.push(`Queue: ${String(engineStatus.queueLength)} pending`);
    lines.push(`Total runs: ${String(engineStatus.totalRunsCompleted)}`);
    lines.push('');
    lines.push('Workers:');
    for (const ws of engineStatus.workers) {
        lines.push(formatWorkerStatus(ws));
    }
    if (engineStatus.workers.length === 0) {
        lines.push('  No workers registered. Call loadRegistry() to initialize.');
    }
    const nextSteps = [];
    if (!engineStatus.active) {
        nextSteps.push('Run `configure_workers` to start the engine');
    }
    if (engineStatus.queueLength > 0) {
        nextSteps.push(`Workers are processing ${String(engineStatus.queueLength)} pending tasks`);
    }
    nextSteps.push('Use `configure_workers` to adjust worker settings');
    lines.push('');
    lines.push('🚀 **Next Steps**');
    nextSteps.forEach((step, i) => lines.push(`${String(i + 1)}. ${step}`));
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=worker-status-handler.js.map