// tools/init-project/migration-runner.ts — Spec migration orchestration during init_project
import { specStore, knowledgeStore } from '../../storage/index.js';
import { filterLegacySpecs, migrateSpecs, filterUnprefixedSpecs, migrateSpecFolderNames, discoverAndFlattenSpecs, reconcileSpecPaths, migrateAllSpecsToLean, } from '../../engine/spec-migrator.js';
/**
 * Run all spec migration passes during init_project:
 * 1. Discover and flatten specs from subcategory folders (v0.30 → v0.40)
 * 2. Migrate legacy docs/sdd/specs/ → planu/specs/
 * 3. Reconcile stale paths in specs.json
 * 4. Migrate spec folders without SPEC-XXX- prefix
 */
export async function runSpecMigrations(projectPath, projectId, knowledge) {
    // Auto-discover and flatten specs in subcategory folders (v0.30 → v0.40 migration)
    let discoveryResult = null;
    try {
        discoveryResult = await discoverAndFlattenSpecs(projectPath);
    }
    catch {
        /* best-effort — don't fail init */
    }
    // Auto-migrate legacy specs if detected (docs/sdd/specs/ → planu/specs/)
    const allSpecs = await specStore.listSpecs(projectId);
    const legacySpecs = filterLegacySpecs(allSpecs);
    let migrationResult = null;
    if (legacySpecs.length > 0) {
        migrationResult = await migrateSpecs(projectPath, legacySpecs, {
            listSpecs: specStore.listSpecs,
            updateSpec: specStore.updateSpec,
        });
        // Update knowledge specLocation after migration (create new object to avoid param mutation)
        await knowledgeStore.saveKnowledge(projectId, { ...knowledge, specLocation: 'planu/specs' });
    }
    // Reconcile specs.json paths with actual filesystem (fixes stale paths after rename/flatten)
    try {
        await reconcileSpecPaths(projectPath, {
            listSpecs: specStore.listSpecs,
            updateSpec: specStore.updateSpec,
        }, projectId);
    }
    catch {
        /* best-effort */
    }
    // Auto-migrate spec folders without SPEC-XXX- prefix
    const unprefixedSpecs = filterUnprefixedSpecs(await specStore.listSpecs(projectId));
    let folderMigrationResult = null;
    if (unprefixedSpecs.length > 0) {
        folderMigrationResult = await migrateSpecFolderNames(projectPath, unprefixedSpecs, {
            listSpecs: specStore.listSpecs,
            updateSpec: specStore.updateSpec,
        });
    }
    // SPEC-461 Phase 2: Auto-migrate old verbose specs to lean format
    try {
        await migrateAllSpecsToLean(projectPath);
    }
    catch {
        /* best-effort — never block init_project */
    }
    // SPEC-630: Merge separate technical.md into unified spec.md
    try {
        const { migrateAllSpecsToUnified } = await import('../../engine/spec-migrator/index.js');
        await migrateAllSpecsToUnified(projectPath).catch(() => null);
    }
    catch {
        /* best-effort — never block init_project */
    }
    // SPEC-1017: strict managed planu/ cleanup after all legacy migrations.
    try {
        const { runStrictPlanuCleanup } = await import('../../engine/spec-migrator/index.js');
        await runStrictPlanuCleanup(projectPath);
    }
    catch {
        /* best-effort — never block init_project */
    }
    return { discoveryResult, migrationResult, folderMigrationResult };
}
/** Return all specs for the project (needed by caller for health check refs). */
export async function listProjectSpecs(projectId) {
    return specStore.listSpecs(projectId);
}
//# sourceMappingURL=migration-runner.js.map