// engine/migration/migration-tracker.ts — Migration status and tracking (SPEC-028b)
// Builds status reports for ongoing migrations
// --- Status calculation ---
function calculateCompletionDate(totalComponents, completedComponents, estimatedTotalHours) {
    if (totalComponents === 0) {
        return new Date().toISOString();
    }
    const remainingFraction = 1 - completedComponents / totalComponents;
    const remainingHours = estimatedTotalHours * remainingFraction;
    const remainingMs = remainingHours * 60 * 60 * 1000;
    return new Date(Date.now() + remainingMs).toISOString();
}
function classifyComponentStatus(componentId, completedIds, inProgressIds, blockedIds) {
    if (completedIds.includes(componentId)) {
        return 'completed';
    }
    if (inProgressIds.includes(componentId)) {
        return 'in-progress';
    }
    if (blockedIds.includes(componentId)) {
        return 'blocked';
    }
    return 'pending';
}
// --- Risk detection ---
function detectActiveRisks(params) {
    const risks = [];
    const { plan, completedComponentIds, activeFlags } = params;
    // Circular dependencies still unresolved
    if (plan.circularDependencies.length > 0) {
        risks.push({
            id: 'circular-deps',
            description: `${plan.circularDependencies.length} circular dependency cycle(s) detected — resolve before migrating affected components`,
            severity: 'high',
            mitigationStatus: 'open',
        });
    }
    // High risk factor
    if (plan.riskFactor > 1.8) {
        risks.push({
            id: 'high-risk-factor',
            description: `Risk factor is ${plan.riskFactor.toFixed(2)} (>1.8) — consider adding tests before proceeding`,
            severity: 'high',
            mitigationStatus: 'open',
        });
    }
    // Late-stage flags (tech debt)
    const techDebtFlags = activeFlags.filter((f) => f.isTechDebt);
    if (techDebtFlags.length > 0) {
        risks.push({
            id: 'flag-tech-debt',
            description: `${techDebtFlags.length} feature flag(s) active for >90 days — remove after migration completes`,
            severity: 'medium',
            mitigationStatus: 'open',
        });
    }
    // Stalled migration (no components completed)
    if (completedComponentIds.length === 0 && plan.specs.length > 0) {
        risks.push({
            id: 'no-progress',
            description: 'Migration started but no components have been completed — review blockers',
            severity: 'medium',
            mitigationStatus: 'open',
        });
    }
    return risks;
}
// --- Public API ---
export function buildMigrationStatusReport(params) {
    const { projectId, plan, completedComponentIds, inProgressComponentIds, blockedComponentIds, activeFlags, completedSpecIds, } = params;
    const allComponentIds = plan.specs.map((s) => s.specId);
    const totalComponents = allComponentIds.length;
    const components = allComponentIds.map((id) => {
        const spec = plan.specs.find((s) => s.specId === id);
        const status = classifyComponentStatus(id, completedComponentIds, inProgressComponentIds, blockedComponentIds);
        return {
            id,
            name: spec?.title ?? id,
            status,
            specId: completedSpecIds?.[id],
            completedAt: status === 'completed' ? new Date().toISOString() : undefined,
        };
    });
    const completedCount = components.filter((c) => c.status === 'completed').length;
    const inProgressCount = components.filter((c) => c.status === 'in-progress').length;
    const pendingCount = components.filter((c) => c.status === 'pending').length;
    const percentComplete = totalComponents > 0 ? Math.round((completedCount / totalComponents) * 100) : 0;
    const activeRisks = detectActiveRisks({
        plan,
        completedComponentIds,
        activeFlags,
    });
    const estimatedCompletionDate = calculateCompletionDate(totalComponents, completedCount, plan.adjustedHours);
    return {
        projectId,
        percentComplete,
        totalComponents,
        completedComponents: completedCount,
        inProgressComponents: inProgressCount,
        pendingComponents: pendingCount,
        components,
        activeRisks,
        activeFlags,
        estimatedCompletionDate,
        generatedAt: new Date().toISOString(),
    };
}
//# sourceMappingURL=migration-tracker.js.map