import { hashProjectPath } from '../storage/base-store.js';
import { getSpec } from '../storage/spec-store.js';
import { getComplianceConfig, saveComplianceConfig } from '../storage/compliance-store.js';
import { detectComplianceContext, checkCompliance } from '../engine/compliance-checker.js';
// ---------------------------------------------------------------------------
// check_compliance
// ---------------------------------------------------------------------------
export async function handleCheckCompliance(input) {
    const projectId = hashProjectPath(input.projectPath);
    const spec = await getSpec(projectId, input.specId);
    if (spec === null) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Spec ${input.specId} not found. Check the ID and try again.`,
                },
            ],
            isError: true,
        };
    }
    // Resolve frameworks: use provided ones, or auto-detect, or fall back to project config
    let frameworks = input.frameworks ?? [];
    if (frameworks.length === 0) {
        const detected = detectComplianceContext(spec.title, '');
        if (detected.length > 0) {
            frameworks = detected;
        }
        else {
            const config = await getComplianceConfig(input.projectPath);
            frameworks = config?.frameworks ?? [];
        }
    }
    if (frameworks.length === 0) {
        return {
            content: [
                {
                    type: 'text',
                    text: 'No compliance frameworks detected or configured. ' +
                        'Provide `frameworks` in the request or run `configure_compliance` first.',
                },
            ],
        };
    }
    const report = await checkCompliance(spec, frameworks);
    const lines = [
        `## Compliance Report — ${spec.id}: ${spec.title}`,
        '',
        `**Frameworks checked:** ${report.frameworks.join(', ')}`,
        `**Coverage:** ${String(report.coveragePercent)}%`,
        `**Total gaps:** ${String(report.gaps.length)}`,
        '',
    ];
    if (report.gaps.length === 0) {
        lines.push('All required compliance criteria are present. No action needed.');
    }
    else {
        lines.push('### Gaps Found', '');
        for (const gap of report.gaps) {
            const icon = gap.status === 'partial' ? '~' : '✗';
            lines.push(`- [${gap.framework.toUpperCase()}] ${icon} ${gap.criterion}`);
        }
        lines.push('');
        lines.push(`Run \`inject_criteria\` or update the spec's acceptance criteria to address these gaps.`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// configure_compliance
// ---------------------------------------------------------------------------
export async function handleConfigureCompliance(input) {
    await saveComplianceConfig(input.projectPath, {
        frameworks: input.frameworks,
        updatedAt: new Date().toISOString(),
    });
    const lines = [
        'Compliance configuration saved.',
        '',
        `**Active frameworks:** ${input.frameworks.join(', ')}`,
        '',
        'Run `check_compliance` on any spec to detect compliance gaps.',
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=compliance-handler.js.map