/**
 * Remove orphaned sessions and clean up stale data.
 *
 * @task T4782
 * @task T2304
 * @epic T4654
 */
import { getRawConfigValue } from '../config.js';
import { getAccessor } from '../store/data-accessor.js';
/** Default auto-end threshold when no config is set (7 days). */
const DEFAULT_AUTO_END_DAYS = 7;
/**
 * Remove orphaned sessions, auto-end stale active sessions, and clean up stale data.
 *
 * Stale active sessions (no activity beyond the configured threshold) are
 * transitioned to 'ended' with an auto-end note. The threshold is read from
 * `retention.autoEndActiveAfterDays` in the project config (default: 7 days).
 *
 * @task T2304
 */
export async function cleanupSessions(projectRoot) {
    const accessor = await getAccessor(projectRoot);
    const sessions = await accessor.loadSessions();
    if (sessions.length === 0) {
        return { removed: [], autoEnded: [], cleaned: false };
    }
    const removed = [];
    const autoEnded = [];
    // Read auto-end threshold from config
    const configDays = await getRawConfigValue('retention.autoEndActiveAfterDays', projectRoot);
    const autoEndDays = typeof configDays === 'number' && configDays > 0 ? configDays : DEFAULT_AUTO_END_DAYS;
    const autoEndMs = autoEndDays * 24 * 60 * 60 * 1000;
    const now = Date.now();
    for (const session of sessions) {
        // Auto-end stale active sessions (T2304)
        if (session.status === 'active') {
            const sessionTime = new Date(session.startedAt).getTime();
            if (now - sessionTime > autoEndMs) {
                session.status = 'ended';
                session.endedAt = new Date().toISOString();
                if (!session.notes)
                    session.notes = [];
                session.notes.push(`Auto-ended: session exceeded ${autoEndDays}-day inactivity threshold`);
                autoEnded.push(session.id);
                await accessor.upsertSingleSession(session);
            }
        }
        // Identify archived sessions to remove
        if (session.status === 'archived') {
            removed.push(session.id);
        }
    }
    // Remove archived sessions
    for (const id of removed) {
        await accessor.removeSingleSession(id);
    }
    return { removed, autoEnded, cleaned: removed.length > 0 || autoEnded.length > 0 };
}
//# sourceMappingURL=session-cleanup.js.map