// tools/spec-diff-handler.ts — Handlers for diff_spec_versions and spec_history (SPEC-284)
import { compactResult } from './output-formatter.js';
import { projectDataDir } from '../storage/index.js';
import { computeSpecVersionDiff, formatDiffAsText, listSpecVersionHistory, } from '../engine/spec-differ.js';
/** Handler for the diff_spec_versions tool. */
export async function handleDiffSpecVersions(args) {
    const dataDir = projectDataDir(args.projectId);
    const format = args.format ?? 'text';
    try {
        const diff = await computeSpecVersionDiff({
            dataDir,
            specId: args.specId,
            fromVersion: args.fromVersion,
            toVersion: args.toVersion,
        });
        const summary = `${diff.criteriaChanges.added.length} criteria added, ` +
            `${diff.criteriaChanges.removed.length} removed, ` +
            `${diff.criteriaChanges.modified.length} modified`;
        if (format === 'json') {
            return {
                content: [
                    {
                        type: 'text',
                        text: JSON.stringify({
                            specId: diff.specId,
                            fromTag: diff.fromTag,
                            toTag: diff.toTag,
                            summary,
                            linesAdded: diff.added.length,
                            linesRemoved: diff.removed.length,
                            criteriaChanges: diff.criteriaChanges,
                            sectionChanges: diff.sectionChanges,
                        }, null, 2),
                    },
                ],
            };
        }
        // text format (default)
        const text = formatDiffAsText(diff);
        return {
            content: [
                {
                    type: 'text',
                    text: `${text}\n\nSummary: ${summary}`,
                },
            ],
        };
    }
    catch (error) {
        const msg = error instanceof Error ? error.message : String(error);
        return { content: [{ type: 'text', text: `Error: ${msg}` }], isError: true };
    }
}
/** Handler for the spec_version_history tool. */
export async function handleSpecVersionHistory(args) {
    const { projectId, specId, limit } = args;
    const dataDir = projectDataDir(projectId);
    try {
        const summaries = await listSpecVersionHistory(dataDir, specId, limit ?? 20);
        if (summaries.length === 0) {
            return compactResult([
                `**specId**: ${specId}`,
                `**totalVersions**: 0`,
                `No version snapshots found for spec "${specId}". Create snapshots with version_spec first.`,
            ].join('\n'));
        }
        const versionLines = summaries
            .slice(0, 20)
            .map((s) => `- ${s.tag} (${s.timestamp}): ${s.changeSummary}`);
        return compactResult([
            `**specId**: ${specId}`,
            `**totalVersions**: ${summaries.length}`,
            `${summaries.length} version snapshot(s) found for spec "${specId}".`,
            '',
            '## Versions',
            ...versionLines,
        ].join('\n'));
    }
    catch (error) {
        const msg = error instanceof Error ? error.message : String(error);
        return { content: [{ type: 'text', text: `Error: ${msg}` }], isError: true };
    }
}
//# sourceMappingURL=spec-diff-handler.js.map