import { HISTORY, LIMITS } from "../../shared/index.js";

/**
 * Progress Store - Session data management
 */

export interface TodoProgress {
    total: number;
    completed: number;
    pending: number;
    percentage: number;
}

export interface TaskProgress {
    total: number;
    running: number;
    completed: number;
    failed: number;
    percentage: number;
}

export interface StepProgress {
    current: number;
    max: number;
}

export interface ProgressSnapshot {
    sessionId: string;
    timestamp: Date;
    todos: TodoProgress;
    tasks: TaskProgress;
    steps: StepProgress;
    startedAt: Date;
    elapsedMs: number;
}

export interface SnapshotInput {
    todoTotal?: number;
    todoCompleted?: number;
    taskTotal?: number;
    taskRunning?: number;
    taskCompleted?: number;
    taskFailed?: number;
    currentStep?: number;
    maxSteps?: number;
}

// Progress history by session
const progressHistory = new Map<string, ProgressSnapshot[]>();
const sessionStartTimes = new Map<string, Date>();

/**
 * Start tracking a session
 */
export function startSession(sessionId: string): void {
    sessionStartTimes.set(sessionId, new Date());
    progressHistory.set(sessionId, []);
}

/**
 * Get session start time
 */
export function getSessionStart(sessionId: string): Date | undefined {
    return sessionStartTimes.get(sessionId);
}

/**
 * Record a progress snapshot
 */
export function recordSnapshot(sessionId: string, data: SnapshotInput): ProgressSnapshot {
    const startedAt = sessionStartTimes.get(sessionId) || new Date();
    const now = new Date();

    const snapshot: ProgressSnapshot = {
        sessionId,
        timestamp: now,
        todos: {
            total: data.todoTotal || 0,
            completed: data.todoCompleted || 0,
            pending: (data.todoTotal || 0) - (data.todoCompleted || 0),
            percentage: data.todoTotal
                ? Math.round((data.todoCompleted || 0) / data.todoTotal * 100)
                : 0,
        },
        tasks: {
            total: data.taskTotal || 0,
            running: data.taskRunning || 0,
            completed: data.taskCompleted || 0,
            failed: data.taskFailed || 0,
            percentage: data.taskTotal
                ? Math.round(((data.taskCompleted || 0) + (data.taskFailed || 0)) / data.taskTotal * 100)
                : 0,
        },
        steps: {
            current: data.currentStep || 0,
            max: data.maxSteps || Infinity,
        },
        startedAt,
        elapsedMs: now.getTime() - startedAt.getTime(),
    };

    const history = progressHistory.get(sessionId) || [];
    history.push(snapshot);

    if (history.length > HISTORY.MAX_PROGRESS) {
        history.shift();
    }

    progressHistory.set(sessionId, history);
    return snapshot;
}

/**
 * Get latest snapshot
 */
export function getLatest(sessionId: string): ProgressSnapshot | undefined {
    const history = progressHistory.get(sessionId);
    return history?.[history.length - 1];
}

/**
 * Get progress history
 */
export function getHistory(sessionId: string, limit: number = LIMITS.DEFAULT_LIST_LIMIT): ProgressSnapshot[] {
    const history = progressHistory.get(sessionId) || [];
    return history.slice(-limit);
}

/**
 * Clear session data
 */
export function clearSession(sessionId: string): void {
    progressHistory.delete(sessionId);
    sessionStartTimes.delete(sessionId);
}
