// tools/llm-provider-status-handler.ts — Handler for llm_provider_status tool.
// SPEC-083 AC-09
import { t, ti } from '../i18n/index.js';
import { getAllProviderDefinitions, getCurrentConfig } from './configure-llm-providers-handler.js';
import { checkAllProvidersHealth } from '../engine/llm-providers/health-checker.js';
import { getAllCostSummaries } from '../engine/llm-providers/cost-tracker.js';
import { optimizeCost } from '../engine/llm-providers/cost-optimizer.js';
/**
 * Handle the llm_provider_status tool call.
 * Shows provider status, health, costs, and recommendations.
 */
export function handleLLMProviderStatus(_args) {
    const definitions = getAllProviderDefinitions();
    const config = getCurrentConfig();
    const healthResults = checkAllProvidersHealth(definitions, 0);
    const costSummaries = getAllCostSummaries();
    const lines = [
        t('tools.llm_provider_status.heading'),
        '',
        ti('tools.llm_provider_status.strategyLabel', { strategy: config.strategy }),
        ti('tools.llm_provider_status.failoverLabel', { order: config.failoverOrder.join(' > ') }),
        '',
        t('tools.llm_provider_status.healthHeading'),
        '',
        t('tools.llm_provider_status.healthTableHeader'),
        t('tools.llm_provider_status.healthTableSeparator'),
    ];
    for (const health of healthResults) {
        const def = definitions.find((d) => d.id === health.providerId);
        const modelCount = def?.models.length ?? 0;
        const cost = costSummaries.find((c) => c.providerId === health.providerId);
        const daily = cost ? `$${cost.daily.toFixed(4)}` : '$0.00';
        const monthly = cost ? `$${cost.monthly.toFixed(4)}` : '$0.00';
        lines.push(`| ${String(health.providerId)} | ${statusEmoji(health.status)} ${health.status} | ${health.apiKeyStatus} | ${String(modelCount)} | ${daily} | ${monthly} |`);
    }
    // Add model details if detailed
    lines.push('', t('tools.llm_provider_status.modelsHeading'), '');
    for (const def of definitions) {
        lines.push(`**${def.name}:**`);
        for (const model of def.models) {
            lines.push(`  - ${model.name} (${model.tier}) — $${String(model.pricingPerMToken.input)}/$${String(model.pricingPerMToken.output)} per 1M tokens (in/out)`);
        }
    }
    // Add recommendation
    const mediumRecs = optimizeCost(definitions, 'medium');
    if (mediumRecs.length > 0) {
        const best = mediumRecs[0];
        if (best) {
            lines.push('', t('tools.llm_provider_status.recommendationHeading'), '', ti('tools.llm_provider_status.recommendation', {
                provider: String(best.providerId),
                model: best.modelId,
                cost: best.costEstimate.usdEstimated.toFixed(2),
            }));
        }
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
function statusEmoji(status) {
    switch (status) {
        case 'healthy':
            return '[OK]';
        case 'degraded':
            return '[WARN]';
        default:
            return '[ERR]';
    }
}
//# sourceMappingURL=llm-provider-status-handler.js.map