// ─────────────────────────────────────────────────────────
// ChefVerse · Course progress store (V2)
// Per-course, per-module, per-chapter completion.
// Sequential locking: module N+1 unlocks ONLY when N is 100%.
// Persists to localStorage so progress survives reload.
// ─────────────────────────────────────────────────────────

const PROGRESS_KEY = "chefverse.progress.v1";

// Chapter counts per (course, moduleIdx) — synthetic but stable.
// The "fundamentals · module 4" row uses the rich 6-chapter ACTIVE_MODULE,
// every other module gets a deterministic 4-chapter list.
function _chapterCount(courseId, moduleIdx) {
  if (courseId === "fundamentals" && moduleIdx === 4) return 6;
  return 4;
}

// ─────────────────────────────────────────────────────────
// Demo seed — chefs land with believable mid-flight progress
// per the COURSES rows in data.jsx. We translate course.progress
// into "module N is current at C/total chapters; everything before is done".
// ─────────────────────────────────────────────────────────
function _seedFromCourses() {
  const out = {};
  // Profile-aware seeding: for new chefs, every course starts at zero.
  // The "experienced" profile retains the believable mid-flight progress
  // baked into COURSES.progress so the demo can show realistic states.
  // We read the profile out of the EDITMODE block in index.html (where
  // useTweaks defaults live) — it's the only source available pre-React.
  let isNewProfile = false;
  try {
    if (typeof window !== "undefined" && window.__CHEFVERSE_PROFILE === "new") {
      isNewProfile = true;
    }
  } catch (e) { /* default false */ }
  (window.COURSES || []).forEach(c => {
    const totalModules = (window.COURSE_SYLLABI?.[c.id] || []).length;
    if (!totalModules) return;
    out[c.id] = { modules: {} };
    if (!c.enrolled) return;
    if (isNewProfile) return; // new chef: nothing seeded
    if (c.progress >= 1) {
      // Fully complete — every chapter checked
      for (let m = 0; m < totalModules; m++) {
        const chapters = _chapterCount(c.id, m);
        out[c.id].modules[m] = {};
        for (let ch = 0; ch < chapters; ch++) out[c.id].modules[m][ch] = true;
      }
      return;
    }
    if (c.progress <= 0) return;
    // Mid-flight: figure out where the chef is
    const doneModules = Math.floor(c.progress * totalModules);
    // Mark prior modules as fully done
    for (let m = 0; m < doneModules; m++) {
      const chapters = _chapterCount(c.id, m);
      out[c.id].modules[m] = {};
      for (let ch = 0; ch < chapters; ch++) out[c.id].modules[m][ch] = true;
    }
    // Current module: partially done — give them ~40% of chapters checked
    if (doneModules < totalModules) {
      const chapters = _chapterCount(c.id, doneModules);
      const chDone = Math.max(1, Math.floor(chapters * 0.4));
      out[c.id].modules[doneModules] = {};
      for (let ch = 0; ch < chDone; ch++) out[c.id].modules[doneModules][ch] = true;
    }
  });
  return out;
}

function _loadProgress() {
  try {
    const raw = localStorage.getItem(PROGRESS_KEY);
    if (raw) return JSON.parse(raw);
  } catch (e) { /* fall through */ }
  return _seedFromCourses();
}

function _saveProgress(state) {
  try { localStorage.setItem(PROGRESS_KEY, JSON.stringify(state)); } catch (e) {}
}

// ─── Pub/sub so every screen re-renders when progress changes ───
const _listeners = new Set();
let _state = null;
function _ensureState() { if (!_state) _state = _loadProgress(); return _state; }
function _set(updater) {
  _state = typeof updater === "function" ? updater(_ensureState()) : updater;
  _saveProgress(_state);
  _listeners.forEach(l => l(_state));
}

// ─────────────────────────────────────────────────────────
// Public API
// ─────────────────────────────────────────────────────────

// Read-only helpers — pure functions on a state snapshot.
function _moduleChaptersDone(state, courseId, moduleIdx) {
  const map = state[courseId]?.modules?.[moduleIdx] || {};
  let done = 0;
  const total = _chapterCount(courseId, moduleIdx);
  for (let i = 0; i < total; i++) if (map[i]) done++;
  return { done, total };
}

function _modulePct(state, courseId, moduleIdx) {
  const { done, total } = _moduleChaptersDone(state, courseId, moduleIdx);
  return total === 0 ? 0 : done / total;
}

function _moduleStatus(state, courseId, moduleIdx) {
  const totalModules = (window.COURSE_SYLLABI?.[courseId] || []).length;
  // Locked unless every previous module is 100%
  for (let m = 0; m < moduleIdx; m++) {
    if (_modulePct(state, courseId, m) < 1) return "locked";
  }
  const pct = _modulePct(state, courseId, moduleIdx);
  if (pct >= 1) return "done";
  if (pct > 0) return "current";
  // Unlocked & not started → "next" if any prior was done, else "current" for module 0
  if (moduleIdx === 0) return "current";
  return "next";
}

function _firstUnlockedUnfinished(state, courseId) {
  const totalModules = (window.COURSE_SYLLABI?.[courseId] || []).length;
  for (let m = 0; m < totalModules; m++) {
    const status = _moduleStatus(state, courseId, m);
    if (status === "current" || status === "next") return m;
  }
  return totalModules - 1; // course done — point at last module
}

function _firstUnfinishedChapter(state, courseId, moduleIdx) {
  const map = state[courseId]?.modules?.[moduleIdx] || {};
  const total = _chapterCount(courseId, moduleIdx);
  for (let i = 0; i < total; i++) if (!map[i]) return i;
  return total - 1;
}

function _courseProgress(state, courseId) {
  const totalModules = (window.COURSE_SYLLABI?.[courseId] || []).length;
  if (!totalModules) return 0;
  let sum = 0;
  for (let m = 0; m < totalModules; m++) sum += _modulePct(state, courseId, m);
  return sum / totalModules;
}

// React hook — subscribes to the store and exposes API.
function useCourseProgress() {
  const [, force] = React.useState(0);
  React.useEffect(() => {
    const fn = () => force(n => n + 1);
    _listeners.add(fn);
    _ensureState();
    return () => _listeners.delete(fn);
  }, []);
  const state = _ensureState();
  return {
    // reads
    isChapterDone: (courseId, m, ch) => !!state[courseId]?.modules?.[m]?.[ch],
    moduleChaptersDone: (courseId, m) => _moduleChaptersDone(state, courseId, m),
    modulePct: (courseId, m) => _modulePct(state, courseId, m),
    moduleStatus: (courseId, m) => _moduleStatus(state, courseId, m),
    firstUnlockedUnfinished: (courseId) => _firstUnlockedUnfinished(state, courseId),
    firstUnfinishedChapter: (courseId, m) => _firstUnfinishedChapter(state, courseId, m),
    courseProgress: (courseId) => _courseProgress(state, courseId),
    chapterCount: (courseId, m) => _chapterCount(courseId, m),

    // writes
    markChapterDone: (courseId, moduleIdx, chapterIdx) => {
      _set(s => {
        const next = { ...s };
        next[courseId] = next[courseId] || { modules: {} };
        next[courseId] = { ...next[courseId], modules: { ...next[courseId].modules } };
        next[courseId].modules[moduleIdx] = { ...(next[courseId].modules[moduleIdx] || {}), [chapterIdx]: true };
        return next;
      });
    },
    resetCourse: (courseId) => {
      _set(s => {
        const next = { ...s };
        delete next[courseId];
        return next;
      });
    },
    resetAll: () => _set(_seedFromCourses()),

    // raw
    _state: state,
  };
}

// Expose for non-React readers (sidebar, dashboard cards, etc.)
window.useCourseProgress = useCourseProgress;
window.getCourseProgressSnapshot = () => _ensureState();
window.markChapterDone = (cid, m, ch) => {
  const api = { markChapterDone: (..._a) => null };
  _set(s => {
    const next = { ...s };
    next[cid] = next[cid] || { modules: {} };
    next[cid] = { ...next[cid], modules: { ...next[cid].modules } };
    next[cid].modules[m] = { ...(next[cid].modules[m] || {}), [ch]: true };
    return next;
  });
};
window.resetCourseProgress = () => _set(_seedFromCourses());
