// tools/performance-analysis-handler.ts — Handler for analyze_performance_impact (SPEC-300)
import { readFile } from 'node:fs/promises';
import { hashProjectPath } from '../storage/base-store.js';
import { specStore } from '../storage/index.js';
import { analyzeSpecPerformance } from '../engine/performance-analyzer.js';
// ---------------------------------------------------------------------------
// Severity emoji map
// ---------------------------------------------------------------------------
const SEVERITY_EMOJI = {
    high: '🔴',
    medium: '🟡',
    low: '🟢',
};
const OVERALL_EMOJI = {
    high: '🔴',
    medium: '🟡',
    low: '🟢',
    none: '✅',
};
// ---------------------------------------------------------------------------
// Formatting
// ---------------------------------------------------------------------------
function formatReport(result) {
    const lines = [];
    const overallEmoji = OVERALL_EMOJI[result.overallRisk] ?? '✅';
    lines.push(`# Performance Impact Analysis — ${result.specId}`);
    lines.push('');
    lines.push(`**Overall Risk**: ${overallEmoji} ${result.overallRisk.toUpperCase()}`);
    lines.push(`**Risks detected**: ${result.riskCount}`);
    lines.push('');
    if (result.riskCount === 0) {
        lines.push('No performance risks detected. The spec looks good from a performance perspective.');
        return lines.join('\n');
    }
    lines.push('## Detected Risks');
    lines.push('');
    for (const [category, risks] of Object.entries(result.risksByCategory)) {
        if (risks.length === 0) {
            continue;
        }
        for (const risk of risks) {
            const emoji = SEVERITY_EMOJI[risk.severity] ?? '';
            lines.push(`### ${emoji} ${category} (${risk.severity})`);
            lines.push('');
            lines.push(`**Issue**: ${risk.description}`);
            lines.push('');
            lines.push(`**Fix**: ${risk.recommendation}`);
            lines.push('');
        }
    }
    if (result.recommendations.length > 0) {
        lines.push('## Recommendations Summary');
        lines.push('');
        for (const rec of result.recommendations) {
            lines.push(`- ${rec}`);
        }
        lines.push('');
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleAnalyzePerformanceImpact(input) {
    const { specId, projectPath } = input;
    const projectId = hashProjectPath(projectPath);
    const spec = await specStore.getSpec(projectId, specId);
    if (!spec) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Spec '${specId}' not found in project at '${projectPath}'. Run init_project first.`,
                },
            ],
            isError: true,
        };
    }
    // Read spec.md content if available
    let specContent = '';
    if (spec.specPath) {
        try {
            specContent = await readFile(spec.specPath, 'utf-8');
        }
        catch {
            // File not readable — fall back to title only
        }
    }
    const result = analyzeSpecPerformance(spec, specContent);
    return {
        content: [{ type: 'text', text: formatReport(result) }],
        structuredContent: result,
    };
}
//# sourceMappingURL=performance-analysis-handler.js.map