export type ProblemStatus = 'solved' | 'attempted' | 'not_started' export interface ProblemProgress { status: ProblemStatus solvedAt?: string attempts: number lastAttemptedAt?: string } export interface ProgressData { problems: Record sessionStats: { solvedToday: number attemptedToday: number currentStreak: number lastSolvedDate?: string } } const STORAGE_KEY = 'codepractice_progress' export function getProgress(): ProgressData { if (typeof window === 'undefined') { return { problems: {}, sessionStats: { solvedToday: 0, attemptedToday: 0, currentStreak: 0 } } } try { const stored = localStorage.getItem(STORAGE_KEY) if (!stored) { return { problems: {}, sessionStats: { solvedToday: 0, attemptedToday: 0, currentStreak: 0 } } } const data = JSON.parse(stored) as ProgressData // Reset daily stats if it's a new day const today = new Date().toDateString() const lastSolvedDate = data.sessionStats.lastSolvedDate if (lastSolvedDate && lastSolvedDate !== today) { data.sessionStats.solvedToday = 0 data.sessionStats.attemptedToday = 0 // Check streak const yesterday = new Date() yesterday.setDate(yesterday.getDate() - 1) if (lastSolvedDate === yesterday.toDateString()) { data.sessionStats.currentStreak += 1 } else { data.sessionStats.currentStreak = 0 } } return data } catch { return { problems: {}, sessionStats: { solvedToday: 0, attemptedToday: 0, currentStreak: 0 } } } } export function saveProgress(data: ProgressData): void { if (typeof window === 'undefined') return try { localStorage.setItem(STORAGE_KEY, JSON.stringify(data)) } catch (error) { console.error('Failed to save progress:', error) } } export function getProblemStatus(questionId: string): ProblemStatus { const progress = getProgress() return progress.problems[questionId]?.status || 'not_started' } export function markProblemAttempted(questionId: string): void { const progress = getProgress() const today = new Date().toDateString() if (!progress.problems[questionId]) { progress.problems[questionId] = { status: 'attempted', attempts: 0, } } const problem = progress.problems[questionId] if (problem.status !== 'solved') { problem.status = 'attempted' problem.attempts += 1 problem.lastAttemptedAt = new Date().toISOString() // Update session stats if (progress.sessionStats.lastSolvedDate !== today) { progress.sessionStats.attemptedToday += 1 } } saveProgress(progress) } export function markProblemSolved(questionId: string): void { const progress = getProgress() const today = new Date().toDateString() if (!progress.problems[questionId]) { progress.problems[questionId] = { status: 'solved', attempts: 1, solvedAt: new Date().toISOString(), } } else { const problem = progress.problems[questionId] problem.status = 'solved' problem.attempts += 1 if (!problem.solvedAt) { problem.solvedAt = new Date().toISOString() } } // Update session stats if (progress.sessionStats.lastSolvedDate !== today) { progress.sessionStats.solvedToday = 0 progress.sessionStats.attemptedToday = 0 } progress.sessionStats.solvedToday += 1 progress.sessionStats.lastSolvedDate = today saveProgress(progress) } export function getCategoryProgress(topic: string, difficulty: string, allQuestions: Array<{ id: string; topic: string; difficulty: string }>): { total: number solved: number attempted: number notStarted: number percentage: number } { const progress = getProgress() const categoryQuestions = allQuestions.filter( q => q.topic === topic && q.difficulty === difficulty ) let solved = 0 let attempted = 0 let notStarted = 0 categoryQuestions.forEach(q => { const status = progress.problems[q.id]?.status || 'not_started' if (status === 'solved') solved++ else if (status === 'attempted') attempted++ else notStarted++ }) const total = categoryQuestions.length const percentage = total > 0 ? Math.round((solved / total) * 100) : 0 return { total, solved, attempted, notStarted, percentage } } export function getOverallProgress(allQuestions: Array<{ id: string }>): { total: number solved: number attempted: number notStarted: number percentage: number } { const progress = getProgress() let solved = 0 let attempted = 0 let notStarted = 0 allQuestions.forEach(q => { const status = progress.problems[q.id]?.status || 'not_started' if (status === 'solved') solved++ else if (status === 'attempted') attempted++ else notStarted++ }) const total = allQuestions.length const percentage = total > 0 ? Math.round((solved / total) * 100) : 0 return { total, solved, attempted, notStarted, percentage } } export function resetProgress(): void { if (typeof window === 'undefined') return localStorage.removeItem(STORAGE_KEY) }