// engine/health/auto-fixer.ts — Auto-fix for health check issues (SPEC-408)
import { specStore } from '../../storage/index.js';
import { computeSpecHealth } from '../validation-loop.js';
async function collectHealthIssues(projectPath, projectId) {
    const specs = await specStore.listSpecs(projectId);
    const issues = [];
    const scores = await Promise.all(specs.map(async (spec) => {
        const score = await computeSpecHealth(projectPath, spec);
        return { spec, score };
    }));
    for (const { spec, score } of scores) {
        if (score.breakdown.specComplete < 20) {
            issues.push({
                type: 'incomplete-spec',
                specId: spec.id,
                specPath: spec.specPath,
                projectPath,
            });
        }
        if (score.breakdown.testsExist < 20) {
            issues.push({ type: 'no-tests', specId: spec.id, projectPath });
        }
        if (score.breakdown.noDrift < 20) {
            issues.push({ type: 'drift-detected', specId: spec.id, projectPath });
        }
    }
    return issues;
}
// ---------------------------------------------------------------------------
// Fix handlers
// ---------------------------------------------------------------------------
function buildSkipForIncompleteSpec(specId) {
    return {
        issue: `incomplete-spec: ${specId}`,
        reason: 'Missing required spec fields cannot be auto-fixed — human authoring required',
        manualSteps: [
            `Open planu/specs/${specId}/spec.md`,
            'Add title, description, and at least one acceptance criterion',
            'Re-run health_check_all to verify',
        ],
    };
}
function buildSkipForNoTests(specId) {
    return {
        issue: `no-tests: ${specId}`,
        reason: 'Test files must be authored by a developer — cannot auto-generate without implementation context',
        manualSteps: [
            `Create tests/ file referencing ${specId}`,
            'Implement at least one test case covering a key acceptance criterion',
            'Run pnpm test to confirm',
        ],
    };
}
function buildSkipForDrift(specId) {
    return {
        issue: `drift-detected: ${specId}`,
        reason: 'Drift requires manual reconciliation between spec and implementation',
        manualSteps: [
            `Run detect_drift with specId: "${specId}" for a detailed report`,
            'Review which criteria are no longer implemented',
            'Either update the spec or fix the code to match',
        ],
    };
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Reads health check output and applies deterministic fixes where possible.
 * When dryRun=true, reports what would be fixed without writing any files.
 */
export async function autoFixHealth(projectPath, projectId, dryRun = false) {
    void dryRun;
    const issues = await collectHealthIssues(projectPath, projectId);
    const fixes = [];
    const skipped = [];
    for (const issue of issues) {
        switch (issue.type) {
            case 'incomplete-spec':
                skipped.push(buildSkipForIncompleteSpec(issue.specId));
                break;
            case 'no-tests':
                skipped.push(buildSkipForNoTests(issue.specId));
                break;
            case 'drift-detected':
                skipped.push(buildSkipForDrift(issue.specId));
                break;
        }
    }
    return {
        fixedCount: fixes.filter((f) => f.success).length,
        skippedCount: skipped.length,
        fixes,
        skipped,
        executedAt: new Date().toISOString(),
    };
}
//# sourceMappingURL=auto-fixer.js.map