// SPEC-452 — TDD Enforcement tool handlers
import { readFile } from 'node:fs/promises';
import { hashProjectPath } from '../storage/base-store.js';
import { getTddPolicy, saveTddPolicy } from '../storage/tdd-policy-store.js';
import { generateTestStubs } from '../engine/tdd-enforcer/stub-generator.js';
import { generateEdgeTests } from '../engine/tdd-enforcer/zod-edge-generator.js';
import { checkTddReadiness, checkTddCompletion } from '../engine/tdd-enforcer/tdd-gate.js';
import { compactResult, formatKeyValue, formatList } from './output-formatter.js';
export async function handleConfigureTddPolicy(params) {
    const projectId = hashProjectPath(params.projectPath);
    const mode = params.mode;
    if (!['off', 'soft', 'hard'].includes(mode)) {
        return {
            content: [{ type: 'text', text: `Invalid mode: ${mode}. Use: off, soft, hard` }],
            isError: true,
        };
    }
    const policy = {
        mode,
        coverageThreshold: params.coverageThreshold ?? 80,
        requireEdgeTests: params.requireEdgeTests ?? false,
    };
    await saveTddPolicy(projectId, policy);
    const message = mode === 'hard'
        ? 'TDD hard mode: implementing transition requires test stubs, done requires all stubs resolved.'
        : mode === 'soft'
            ? 'TDD soft mode: warnings on missing stubs, no blocking.'
            : 'TDD enforcement disabled.';
    return compactResult(formatKeyValue({
        status: 'configured',
        mode: policy.mode,
        coverageThreshold: policy.coverageThreshold,
        message,
    }));
}
export async function handleGenerateEdgeTests(params) {
    if (params.specId) {
        const result = await generateTestStubs(params.specId, params.projectPath);
        if (result.stubs.length === 0) {
            return {
                content: [
                    { type: 'text', text: `No acceptance criteria found in ${params.specId} spec.md` },
                ],
            };
        }
        return compactResult(formatKeyValue({
            specId: params.specId,
            stubCount: result.stubs.length,
            outputPath: result.outputPath,
            message: `Generated ${result.stubs.length} TDD stubs → ${result.outputPath}`,
        }));
    }
    if (params.registerFile) {
        const content = await readFile(params.registerFile, 'utf-8');
        const toolMatch = /['"](\w+)['"]\s*,/.exec(content);
        const toolName = toolMatch?.[1] ?? 'unknown';
        const result = await generateEdgeTests(toolName, params.registerFile, content);
        return compactResult([
            formatKeyValue({
                toolName,
                edgeCaseCount: result.cases.length,
                message: `Generated ${result.cases.length} edge-case tests for ${toolName}`,
            }),
            '',
            formatList(result.cases.map((c) => c.description), 'Edge Cases'),
        ].join('\n'));
    }
    return { content: [{ type: 'text', text: 'Provide specId or registerFile' }], isError: true };
}
export async function handleTddStatus(params) {
    const projectId = hashProjectPath(params.projectPath);
    const policy = await getTddPolicy(projectId);
    if (params.specId) {
        const readiness = await checkTddReadiness(params.specId, params.projectPath);
        const completion = await checkTddCompletion(params.specId, params.projectPath);
        return compactResult(formatKeyValue({
            specId: params.specId,
            mode: policy.mode,
            coverageThreshold: policy.coverageThreshold,
            hasStubs: readiness.hasStubs,
            stubCount: readiness.stubCount,
            unresolvedStubs: completion.unresolvedStubs,
            resolvedPercentage: completion.resolvedPercentage,
        }));
    }
    return compactResult(formatKeyValue({
        mode: policy.mode,
        coverageThreshold: policy.coverageThreshold,
        requireEdgeTests: policy.requireEdgeTests,
        message: `TDD policy: ${policy.mode} mode, ${policy.coverageThreshold}% coverage threshold`,
    }));
}
//# sourceMappingURL=tdd-handler.js.map