// engine/spec-migrator/ssr-back-migration.ts — SPEC-752 orchestrator
// Finds all legacy multi-file spec dirs and folds technical.md / progress.md into spec.md.
import { findLegacyMultiFileSpecs } from './find-legacy-multifile-specs.js';
import { foldTechnicalIntoSpec } from './fold-technical.js';
import { foldProgressIntoSpec } from './fold-progress.js';
async function processTechnical(spec, projectPath, dryRun) {
    if (!spec.hasTechnicalMd) {
        return null;
    }
    if (dryRun) {
        return {
            specId: spec.specId,
            specDir: spec.specDir,
            migrated: true,
            action: 'fold_technical',
            reason: 'ok',
            byteDelta: 0,
        };
    }
    const r = await foldTechnicalIntoSpec(spec.specDir, projectPath);
    if (r.reason === 'already_unified') {
        return {
            specId: spec.specId,
            specDir: spec.specDir,
            migrated: false,
            action: 'noop',
            reason: 'already_unified',
        };
    }
    if (r.ok) {
        return {
            specId: spec.specId,
            specDir: spec.specDir,
            migrated: true,
            action: 'fold_technical',
            reason: 'ok',
            byteDelta: r.byteDelta,
        };
    }
    return {
        specId: spec.specId,
        specDir: spec.specDir,
        migrated: false,
        action: 'noop',
        reason: 'error',
        error: r.error,
    };
}
async function processProgress(spec, projectPath, dryRun, foldProgressMd) {
    if (!spec.hasProgressMd || !foldProgressMd) {
        return null;
    }
    if (dryRun) {
        return {
            specId: spec.specId,
            specDir: spec.specDir,
            migrated: true,
            action: 'fold_progress',
            reason: 'ok',
            byteDelta: 0,
        };
    }
    const p = await foldProgressIntoSpec(spec.specDir, { dropBoilerplate: true }, projectPath);
    if (p.reason === 'dropped_boilerplate') {
        return {
            specId: spec.specId,
            specDir: spec.specDir,
            migrated: true,
            action: 'drop_boilerplate_progress',
            reason: 'ok',
            byteDelta: 0,
        };
    }
    if (p.ok && p.reason === 'ok') {
        return {
            specId: spec.specId,
            specDir: spec.specDir,
            migrated: true,
            action: 'fold_progress',
            reason: 'ok',
            byteDelta: p.byteDelta,
        };
    }
    return null;
}
/**
 * Run the SSR back-migration for a project.
 * - Discovers all spec dirs with technical.md or progress.md.
 * - Skips done-status specs when allowDoneSpecs is false (default).
 * - In dryRun mode, reports what WOULD happen without mutating any file.
 */
export async function runSsrBackMigration(opts) {
    const { projectPath, dryRun, foldProgressMd, allowDoneSpecs } = opts;
    const legacySpecs = await findLegacyMultiFileSpecs(projectPath);
    const report = {
        scanned: legacySpecs.length,
        migrated: 0,
        alreadyUnified: 0,
        skipped: [],
        mutations: [],
        executed: !dryRun,
    };
    for (const spec of legacySpecs) {
        if (spec.isDone && !allowDoneSpecs) {
            report.skipped.push({ specId: spec.specId, reason: 'frozen_done' });
            continue;
        }
        const results = [];
        const tech = await processTechnical(spec, projectPath, dryRun);
        if (tech) {
            results.push(tech);
        }
        const prog = await processProgress(spec, projectPath, dryRun, foldProgressMd);
        if (prog) {
            results.push(prog);
        }
        let specMigrated = false;
        for (const r of results) {
            if (r.reason === 'already_unified') {
                report.alreadyUnified++;
            }
            else if (r.migrated) {
                specMigrated = true;
                report.mutations.push({
                    specId: r.specId,
                    action: r.action,
                    byteDelta: r.byteDelta ?? 0,
                });
            }
        }
        if (specMigrated) {
            report.migrated++;
        }
    }
    return report;
}
//# sourceMappingURL=ssr-back-migration.js.map