import { reconcileSpec } from '../engine/living-specs/index.js';
/**
 * Format the LivingSpecReconcileResult as readable markdown for the tool response.
 */
export async function handleReconcileSpecLiving(input) {
    const { specId, projectPath } = input;
    try {
        const result = await reconcileSpec(specId, projectPath);
        const coveragePct = result.criteriaTotal > 0 ? Math.round((result.criteriaMet / result.criteriaTotal) * 100) : 0;
        const lines = [
            `## Reconcile Result — ${result.specId}`,
            '',
            `**Reconciled at:** ${result.reconciledAt}`,
            `**Files found:** ${result.filesFound}`,
            `**Files missing:** ${result.filesMissing}`,
            `**Criteria met:** ${result.criteriaMet}/${result.criteriaTotal} (${coveragePct}%)`,
        ];
        if (result.driftFlags.length > 0) {
            lines.push('', '### Drift Flags', '');
            for (const flag of result.driftFlags) {
                lines.push(`- ${flag}`);
            }
        }
        if (result.fileResults.length > 0) {
            lines.push('', '### File Results', '');
            for (const fr of result.fileResults) {
                const status = fr.exists ? 'found' : 'MISSING';
                const exports = fr.exportsFound.length > 0 ? ` (exports: ${fr.exportsFound.join(', ')})` : '';
                lines.push(`- **${fr.path}** — ${status}${fr.exists ? `, ${fr.lineCount} lines${exports}` : ''}`);
            }
        }
        if (result.criteriaResults.length > 0) {
            lines.push('', '### Criteria Coverage', '');
            for (const cr of result.criteriaResults) {
                const icon = cr.met ? '[x]' : '[ ]';
                lines.push(`- ${icon} ${cr.criterion} — _${cr.evidence}_`);
            }
        }
        lines.push('', '_spec.md Progress section updated with auto-reconcile data._');
        return {
            content: [{ type: 'text', text: lines.join('\n') }],
        };
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        return {
            content: [{ type: 'text', text: `Error reconciling spec ${specId}: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=reconcile-spec-living-handler.js.map