// tools/reality-check.ts — Feasibility validation tool handler (SPEC-040)
import { knowledgeStore } from '../storage/index.js';
import { validateFeasibility } from '../engine/feasibility-validator.js';
// ---------------------------------------------------------------------------
// Main handler
// ---------------------------------------------------------------------------
export async function handleRealityCheck(args) {
    const { projectId, requirements } = args;
    try {
        if (requirements.length === 0) {
            return errorResult('At least one requirement must be provided.');
        }
        const knowledge = await knowledgeStore.getKnowledge(projectId);
        if (!knowledge) {
            return errorResult(`Project "${projectId}" not found. Run init_project first.`);
        }
        const report = validateFeasibility(requirements, knowledge);
        const text = formatReport(report, requirements.length);
        return { content: [{ type: 'text', text }] };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return errorResult(`reality_check error: ${message}`);
    }
}
// ---------------------------------------------------------------------------
// Formatting
// ---------------------------------------------------------------------------
function formatReport(report, reqCount) {
    const scoreBar = buildScoreBar(report.score);
    const viabilityBadge = report.viable ? 'VIABLE' : 'AT RISK';
    const header = `## Reality Check Report\n\n` +
        `**Requirements evaluated**: ${reqCount}\n` +
        `**Feasibility score**: ${report.score}/100 ${scoreBar}\n` +
        `**Status**: **${viabilityBadge}**\n\n`;
    const warningBlock = !report.viable
        ? `> **WARNING**: Score below 50. Address the blockers below before proceeding.\n\n`
        : '';
    const summaryBlock = `### Summary\n${report.summary}\n\n`;
    const issuesBlock = report.issues.length === 0
        ? `### Issues\nNo significant feasibility issues detected.\n\n`
        : `### Issues\n\n${report.issues.map(formatIssue).join('\n\n')}\n\n`;
    const contextBlock = report.contextUsed.length > 0
        ? `### Context Used\n${report.contextUsed.map((c) => `- ${c}`).join('\n')}\n\n`
        : '';
    const disclaimerBlock = `---\n_${report.disclaimer}_\n`;
    return header + warningBlock + summaryBlock + issuesBlock + contextBlock + disclaimerBlock;
}
function formatIssue(issue) {
    const severityIcon = issue.severity === 'blocker'
        ? 'BLOCKER'
        : issue.severity === 'warning'
            ? 'WARNING'
            : 'SUGGESTION';
    return (`#### [${severityIcon}] ${issue.type.toUpperCase()}\n` +
        `**Problem**: ${issue.description}\n\n` +
        `**Suggested alternative**: ${issue.suggestedAlternative}`);
}
function buildScoreBar(score) {
    const filled = Math.round(score / 10);
    const empty = 10 - filled;
    return `[${'█'.repeat(filled)}${'░'.repeat(empty)}]`;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function errorResult(message) {
    return { content: [{ type: 'text', text: message }], isError: true };
}
//# sourceMappingURL=reality-check.js.map