/**
 * Compute session statistics.
 *
 * @task T4782
 * @epic T4654
 */
import { ExitCode } from '@cleocode/contracts';
import { CleoError } from '../errors.js';
import { getAccessor } from '../store/data-accessor.js';
/**
 * Compute session statistics, optionally for a specific session.
 * Throws CleoError if a specific session is requested but not found.
 */
export async function getSessionStats(projectRoot, sessionId) {
    const accessor = await getAccessor(projectRoot);
    const allSessions = await accessor.loadSessions();
    // If specific session requested
    if (sessionId) {
        const session = allSessions.find((s) => s.id === sessionId);
        if (!session) {
            throw new CleoError(ExitCode.SESSION_NOT_FOUND, `Session '${sessionId}' not found`);
        }
        const startedAt = new Date(session.startedAt).getTime();
        const endedAt = session.endedAt ? new Date(session.endedAt).getTime() : Date.now();
        const durationMinutes = Math.round((endedAt - startedAt) / (1000 * 60));
        return {
            totalSessions: allSessions.length,
            activeSessions: allSessions.filter((s) => s.status === 'active').length,
            suspendedSessions: allSessions.filter((s) => s.status === 'suspended').length,
            endedSessions: allSessions.filter((s) => s.status === 'ended').length,
            archivedSessions: allSessions.filter((s) => s.status === 'archived').length,
            totalTasksCompleted: allSessions.reduce((sum, s) => sum + (s.stats?.tasksCompleted ?? 0), 0),
            totalFocusChanges: allSessions.reduce((sum, s) => sum + (s.stats?.focusChanges ?? 0), 0),
            averageResumeCount: allSessions.length > 0
                ? Math.round((allSessions.reduce((sum, s) => sum + (s.resumeCount ?? 0), 0) / allSessions.length) *
                    100) / 100
                : 0,
            session: {
                id: session.id,
                status: session.status,
                tasksCompleted: session.stats?.tasksCompleted ?? 0,
                focusChanges: session.stats?.focusChanges ?? 0,
                resumeCount: session.resumeCount ?? 0,
                durationMinutes,
            },
        };
    }
    const activeSessions = allSessions.filter((s) => s.status === 'active').length;
    const suspendedSessions = allSessions.filter((s) => s.status === 'suspended').length;
    const endedSessions = allSessions.filter((s) => s.status === 'ended').length;
    const archivedSessions = allSessions.filter((s) => s.status === 'archived').length;
    const totalTasksCompleted = allSessions.reduce((sum, s) => sum + (s.stats?.tasksCompleted ?? 0), 0);
    const totalFocusChanges = allSessions.reduce((sum, s) => sum + (s.stats?.focusChanges ?? 0), 0);
    const averageResumeCount = allSessions.length > 0
        ? Math.round((allSessions.reduce((sum, s) => sum + (s.resumeCount ?? 0), 0) / allSessions.length) *
            100) / 100
        : 0;
    return {
        totalSessions: allSessions.length,
        activeSessions,
        suspendedSessions,
        endedSessions,
        archivedSessions,
        totalTasksCompleted,
        totalFocusChanges,
        averageResumeCount,
    };
}
//# sourceMappingURL=session-stats.js.map