"use client";

const STORAGE_KEY = "kovak_academy_progress";

export function getCompletedModules(): string[] {
  if (typeof window === "undefined") return [];
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    return raw ? JSON.parse(raw) : [];
  } catch {
    return [];
  }
}

export function markModuleComplete(moduleId: string): void {
  const completed = getCompletedModules();
  if (!completed.includes(moduleId)) {
    localStorage.setItem(STORAGE_KEY, JSON.stringify([...completed, moduleId]));
  }
}

export function resetProgress(): void {
  localStorage.removeItem(STORAGE_KEY);
}

export function getProgressPercent(totalModules: number): number {
  const completed = getCompletedModules();
  if (totalModules === 0) return 0;
  return Math.round((completed.length / totalModules) * 100);
}
