// tools/migrate-tech-advanced/rollback-handlers.ts — SPEC-028b rollback, feature flags, and status handlers
import { t } from '../../i18n/index.js';
import { scoreMigratableComponents, generateMigrationPlan, generateRollbackPlan, createMigrationCheckpoint, buildRollbackExecutionPlan, generateFeatureFlagSpec, buildMigrationStatusReport, } from '../../engine/migration/index.js';
export function handleRollbackPlan(args) {
    const components = scoreMigratableComponents([
        {
            id: 'utils',
            name: 'Utils',
            path: './src/utils',
            type: 'util',
            cyclomaticComplexity: 3,
            couplingScore: 1,
            dependsOn: [],
            dependents: ['service'],
            linesOfCode: 100,
        },
        {
            id: 'service',
            name: 'Service',
            path: './src/service',
            type: 'service',
            cyclomaticComplexity: 10,
            couplingScore: 4,
            dependsOn: ['utils'],
            dependents: ['handler'],
            linesOfCode: 300,
        },
        {
            id: 'handler',
            name: 'Handler',
            path: './src/handler',
            type: 'handler',
            cyclomaticComplexity: 6,
            couplingScore: 3,
            dependsOn: ['service'],
            dependents: [],
            linesOfCode: 150,
        },
    ]);
    const plan = generateMigrationPlan({
        sourceStack: args.sourceStack ?? 'unknown',
        targetStack: args.targetStack ?? 'unknown',
        components,
        hasTests: false,
    });
    const sortedOrder = components.map((c) => c.id);
    const data = generateRollbackPlan({ plan, components, sortedOrder });
    const irreversibleCount = data.steps.filter((s) => s.reversibility === 'irreversible').length;
    const warnings = [];
    if (irreversibleCount > 0) {
        warnings.push(`${irreversibleCount} irreversible step(s) detected — these require explicit human confirmation`);
    }
    return {
        action: 'rollback-plan',
        data,
        message: t('tools.migrate_tech.rollbackPlanSuccess'),
        warnings,
        savedTo: args.projectId ? `data/projects/${args.projectId}/rollback-plan.json` : undefined,
    };
}
export function handleCheckpoint(args) {
    const data = createMigrationCheckpoint({
        projectId: args.projectId,
        label: args.component ?? `checkpoint-${Date.now()}`,
        completedComponents: [],
        dbSchemaVersion: 'current',
        serviceConfig: {},
    });
    return {
        action: 'checkpoint',
        data,
        message: t('tools.migrate_tech.checkpointSuccess'),
        warnings: [],
        savedTo: args.projectId
            ? `data/projects/${args.projectId}/migration-checkpoints.json`
            : undefined,
    };
}
export function handleRollback(args) {
    const components = scoreMigratableComponents([
        {
            id: 'service',
            name: 'Service',
            path: './src/service',
            type: 'service',
            cyclomaticComplexity: 10,
            couplingScore: 4,
            dependsOn: [],
            dependents: [],
            linesOfCode: 300,
        },
    ]);
    const plan = generateMigrationPlan({
        sourceStack: args.sourceStack ?? 'unknown',
        targetStack: args.targetStack ?? 'unknown',
        components,
        hasTests: false,
    });
    const rollbackPlan = generateRollbackPlan({
        plan,
        components,
        sortedOrder: components.map((c) => c.id),
    });
    const data = buildRollbackExecutionPlan({
        targetCheckpointId: args.existingAnalysisId ?? 'cp-latest',
        rollbackPlan,
    });
    const warnings = [];
    if (data.requiresManualConfirmation) {
        warnings.push('This rollback requires manual confirmation at critical steps — do not automate without review');
    }
    return {
        action: 'rollback',
        data,
        message: t('tools.migrate_tech.rollbackSuccess'),
        warnings,
    };
}
export function handleFeatureFlags(args) {
    const components = scoreMigratableComponents([
        {
            id: 'app-core',
            name: 'Application Core',
            path: args.projectPath ?? './src',
            type: 'module',
            cyclomaticComplexity: 8,
            couplingScore: 5,
            dependsOn: [],
            dependents: [],
            linesOfCode: 400,
        },
    ]);
    const migrationName = args.sourceStack && args.targetStack
        ? `${args.sourceStack}_to_${args.targetStack}`
        : 'migration';
    const data = generateFeatureFlagSpec({
        migrationName,
        migratedComponents: components,
        hasBaselineMetrics: true,
        strategy: 'strangler-fig',
    });
    const warnings = [
        'Set hasBaselineMetrics=false if source system has no error rate metrics — auto-rollback will be disabled',
        'Remove feature flags after migration is stable (see expirationPolicy in result)',
    ];
    return {
        action: 'feature-flags',
        data,
        message: t('tools.migrate_tech.featureFlagsSuccess'),
        warnings,
    };
}
export function handleStatus(args) {
    const components = scoreMigratableComponents([
        {
            id: 'utils',
            name: 'Utils',
            path: './src/utils',
            type: 'util',
            cyclomaticComplexity: 3,
            couplingScore: 1,
            dependsOn: [],
            dependents: ['service'],
            linesOfCode: 100,
        },
        {
            id: 'service',
            name: 'Service',
            path: './src/service',
            type: 'service',
            cyclomaticComplexity: 10,
            couplingScore: 4,
            dependsOn: ['utils'],
            dependents: ['handler'],
            linesOfCode: 300,
        },
        {
            id: 'handler',
            name: 'Handler',
            path: './src/handler',
            type: 'handler',
            cyclomaticComplexity: 6,
            couplingScore: 3,
            dependsOn: ['service'],
            dependents: [],
            linesOfCode: 150,
        },
    ]);
    const plan = generateMigrationPlan({
        sourceStack: args.sourceStack ?? 'unknown',
        targetStack: args.targetStack ?? 'unknown',
        components,
        hasTests: false,
    });
    const data = buildMigrationStatusReport({
        projectId: args.projectId,
        plan,
        completedComponentIds: [],
        inProgressComponentIds: ['migration-utils'],
        blockedComponentIds: [],
        activeFlags: [],
    });
    const warnings = [];
    if (data.percentComplete === 0) {
        warnings.push('No components completed yet — update completedComponentIds as specs are finished');
    }
    return {
        action: 'status',
        data,
        message: t('tools.migrate_tech.statusSuccess'),
        warnings,
    };
}
//# sourceMappingURL=rollback-handlers.js.map