import { knowledgeStore, specStore } from '../storage/index.js';
import { buildConflictReport, formatConflictReport } from '../engine/contradiction-detector.js';
import { readFile } from 'node:fs/promises';
// ---------------------------------------------------------------------------
// Content loading
// ---------------------------------------------------------------------------
/**
 * Attempt to load the HU.md content for a spec.
 * Returns empty string if the file cannot be read.
 */
async function loadHuContent(huPath) {
    try {
        return await readFile(huPath, 'utf-8');
    }
    catch {
        return '';
    }
}
// ---------------------------------------------------------------------------
// Main handler
// ---------------------------------------------------------------------------
export async function handleDetectContradictions(params) {
    const { projectId } = params;
    try {
        const knowledge = await knowledgeStore.getKnowledge(projectId);
        if (!knowledge) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `Project "${projectId}" not found. Run init_project first.`,
                    },
                ],
                isError: true,
            };
        }
        const specs = await specStore.listSpecs(projectId);
        if (specs.length < 2) {
            return {
                content: [
                    {
                        type: 'text',
                        text: JSON.stringify({
                            projectId,
                            specsAnalyzed: specs.length,
                            contradictions: [],
                            summary: 'At least 2 specs are required to detect contradictions.',
                            blockerCount: 0,
                            warningCount: 0,
                        }, null, 2),
                    },
                ],
            };
        }
        // Load HU.md content for each spec in parallel
        const specInputs = await Promise.all(specs.map(async (spec) => ({
            id: spec.id,
            title: spec.title,
            content: await loadHuContent(spec.huPath),
        })));
        const report = buildConflictReport(specInputs);
        const markdown = formatConflictReport(report);
        const result = {
            projectId,
            specsAnalyzed: specs.length,
            contradictions: report.contradictions,
            specsWithConflicts: report.specsWithConflicts,
            blockerCount: report.blockerCount,
            warningCount: report.warningCount,
            summary: report.summary,
            report: markdown,
        };
        return {
            content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
        };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Internal error: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=detect-contradictions.js.map