/**
 * Nexus Prime — Memory Promotion Engine
 *
 * Promotion rules:
 *   - scratch → session_learning (always allowed)
 *   - session_learning → project_fact (requires 1+ validation)
 *   - project_fact → reusable_pattern (requires 3+ confirmed runs, 0 contradictions)
 *   - scratch → reusable_pattern (NEVER — must go through intermediate classes)
 *   - project_fact can be demoted if contradicted
 *   - session_learning expires after 7 days without validation
 */
import { classifyMemory, isPromotionAllowed, shouldExpireSessionLearning, } from './memory-classes.js';
/**
 * Evaluate promotion/demotion/expiry for a set of memory candidates.
 */
export function evaluatePromotions(candidates, nowMs = Date.now()) {
    const decisions = [];
    for (const c of candidates) {
        const ageMs = nowMs - c.createdAt;
        const newClass = classifyMemory({
            validationCount: c.validationCount,
            contradictionCount: c.contradictionCount,
            ageMs,
            sourceRunCount: c.sourceRunIds.length,
        });
        // No change
        if (newClass === c.currentClass) {
            // But check session_learning expiry
            if (c.currentClass === 'session_learning' && shouldExpireSessionLearning(c.lastValidatedAt, c.validationCount, nowMs)) {
                decisions.push({
                    memoryId: c.memoryId,
                    fromClass: c.currentClass,
                    toClass: 'scratch',
                    action: 'expire',
                    reason: 'Session learning expired — unvalidated for 7+ days',
                });
            }
            continue;
        }
        // Check if transition is allowed
        if (!isPromotionAllowed(c.currentClass, newClass)) {
            decisions.push({
                memoryId: c.memoryId,
                fromClass: c.currentClass,
                toClass: c.currentClass,
                action: 'hold',
                reason: `Promotion from ${c.currentClass} to ${newClass} not allowed (must go through intermediate classes)`,
            });
            continue;
        }
        const ORDER = {
            scratch: 0,
            session_learning: 1,
            project_fact: 2,
            reusable_pattern: 3,
        };
        const action = ORDER[newClass] > ORDER[c.currentClass] ? 'promote' : 'demote';
        const reason = action === 'demote'
            ? `Demoted: ${c.contradictionCount} contradiction(s) vs ${c.validationCount} validation(s)`
            : `Promoted: ${c.validationCount} validation(s) from ${c.sourceRunIds.length} run(s)`;
        decisions.push({
            memoryId: c.memoryId,
            fromClass: c.currentClass,
            toClass: newClass,
            action,
            reason,
        });
    }
    return decisions;
}
