// ─────────────────────────────────────────────────────────
// ChefVerse · Nested curriculum model (V3)
//
// Replaces the flat video-only chapter list with a nested,
// multi-tiered hierarchy supporting four lesson types.
//
// SHAPE (CMS / Strapi-ready):
//   module {
//     id, num, title{locale}, intro{locale},
//     directLessons[]   // lessons attached straight to the module (no sub-module)
//     subModules[] {
//       id, num, title{locale},
//       lessons[]
//     }
//   }
//   lesson {
//     id, title{locale},
//     type: "video" | "written" | "graphic" | "carousel",
//     dur            // seconds (video) — used for the time label; 0 for non-video
//     cards          // carousel only: number of slides to navigate
//     completion: "watch" | "scroll" | "load" | "carousel-all"
//   }
//
// loadCurriculum() returns this synchronously today; swap to a
// fetch() against Strapi later with zero downstream changes.
// ─────────────────────────────────────────────────────────

const LESSON_COMPLETION = {
  video: "watch",       // finish the video
  written: "scroll",    // scroll to the bottom of the article
  graphic: "load",      // single infographic — done on view
  carousel: "carousel-all", // must navigate through every card
};

// Helper to make a lesson with the right completion rule baked in
function _lesson(id, type, title, opts) {
  opts = opts || {};
  return {
    id,
    type,
    title,
    dur: opts.dur || 0,
    cards: opts.cards || (type === "carousel" ? 5 : 0),
    completion: LESSON_COMPLETION[type],
  };
}

// ── The Phase-1 chef journey (per the client's content map) ──
const CURRICULUM = [
  {
    id: "m1",
    num: "1",
    thumb: "https://images.unsplash.com/photo-1556910103-1c02745aae4d?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
    title: { en: "Intro to the platform & UFS", de: "Einführung in Plattform & UFS", id: "Pengantar platform & UFS" },
    intro: {
      en: "Welcome to ChefVerse. This module introduces how the platform works, what's in it for you, and how UFS supports your growth beyond the kitchen.",
      de: "Willkommen bei ChefVerse. Dieses Modul stellt vor, wie die Plattform funktioniert, was für dich drin ist und wie UFS dein Wachstum über die Küche hinaus unterstützt.",
      id: "Selamat datang di ChefVerse. Modul ini memperkenalkan cara kerja platform, apa manfaatnya untukmu, dan bagaimana UFS mendukung pertumbuhanmu di luar dapur.",
    },
    outcomes: {
      en: ["How the ChefVerse platform works end to end", "What you earn and how points and rewards work", "How UFS supports your growth beyond the kitchen"],
      de: ["Wie die ChefVerse-Plattform von A bis Z funktioniert", "Was du verdienst und wie Punkte und Pr\u00E4mien funktionieren", "Wie UFS dein Wachstum \u00FCber die K\u00FCche hinaus unterst\u00FCtzt"],
      id: ["Cara kerja platform ChefVerse dari awal sampai akhir", "Apa yang kamu dapat dan cara kerja poin dan hadiah", "Bagaimana UFS mendukung pertumbuhanmu di luar dapur"],
    },
    directLessons: [
      _lesson("m1-l1", "video", { en: "Intro to the platform & UFS", de: "Einführung in Plattform & UFS", id: "Pengantar platform & UFS" }, { dur: 300 }),
    ],
    subModules: [],
  },
  {
    id: "m2",
    num: "2",
    thumb: "https://images.unsplash.com/photo-1559847844-5315695dadae?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
    title: { en: "From setup to success", de: "Von Setup zu Erfolg", id: "Dari setup ke sukses" },
    intro: {
      en: "The complete content-creation craft, broken into three stages: choosing your platform, filming and editing, and integrating products naturally.",
      de: "Das komplette Content-Handwerk in drei Stufen: Plattform wählen, filmen und schneiden, Produkte natürlich integrieren.",
      id: "Keahlian pembuatan konten lengkap, dibagi tiga tahap: memilih platform, syuting dan edit, serta integrasi produk secara natural.",
    },
    outcomes: {
      en: ["The three stages of creating great chef content", "How to choose, film, and edit for your platform", "How to feature UFS products naturally, not as ads"],
      de: ["Die drei Phasen f\u00FCr starken Chef-Content", "Wie du f\u00FCr deine Plattform ausw\u00E4hlst, filmst und schneidest", "Wie du UFS-Produkte nat\u00FCrlich einbindest, nicht als Werbung"],
      id: ["Tiga tahap membuat konten chef yang bagus", "Cara memilih, syuting, dan edit untuk platformmu", "Cara menampilkan produk UFS secara natural, bukan seperti iklan"],
    },
    directLessons: [],
    subModules: [
      {
        id: "m2-1",
        num: "2.1",
        thumb: "https://images.unsplash.com/photo-1565299624946-b28f40a0ae38?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
        title: { en: "Pick your platform", de: "Wähle deine Plattform", id: "Pilih platform kamu" },
        intro: {
          en: "Every platform rewards a different style. This part helps you pick where to post and shape content that fits how people watch there.",
          de: "Jede Plattform belohnt einen anderen Stil. Dieser Teil hilft dir zu w\u00E4hlen, wo du postest, und Content zu formen, der dort passt.",
          id: "Setiap platform menghargai gaya berbeda. Bagian ini membantumu memilih tempat posting dan membentuk konten yang cocok di sana.",
        },
        outcomes: {
          en: ["Which platform suits your style and audience", "How to format snacks vs full courses", "How to write captions and measure what works"],
          de: ["Welche Plattform zu deinem Stil und Publikum passt", "Wie du Snacks vs. ganze Men\u00FCs formatierst", "Wie du Captions schreibst und misst, was funktioniert"],
          id: ["Platform mana yang cocok dengan gaya dan audiensmu", "Cara memformat camilan vs hidangan lengkap", "Cara menulis caption dan mengukur yang berhasil"],
        },
        lessons: [
          _lesson("m2-1-l1", "graphic", { en: "Pick your platform", de: "Wähle deine Plattform", id: "Pilih platform kamu" }),
          _lesson("m2-1-l2", "video", { en: "Pick your platform", de: "Wähle deine Plattform", id: "Pilih platform kamu" }, { dur: 90 }),
          _lesson("m2-1-l3", "video", { en: "Serving snacks vs courses", de: "Snacks vs. Menüs servieren", id: "Sajikan camilan vs hidangan" }, { dur: 60 }),
          _lesson("m2-1-l4", "written", { en: "Plating your words", de: "Deine Worte anrichten", id: "Menata kata-katamu" }),
          _lesson("m2-1-l5", "video", { en: "Measuring success", de: "Erfolg messen", id: "Mengukur sukses" }, { dur: 60 }),
          _lesson("m2-1-l6", "graphic", { en: "Measuring success", de: "Erfolg messen", id: "Mengukur sukses" }),
        ],
      },
      {
        id: "m2-2",
        num: "2.2",
        thumb: "https://images.unsplash.com/photo-1432139509613-5c4255815697?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
        title: { en: "Filming setup", de: "Film-Setup", id: "Setup syuting" },
        intro: {
          en: "Great content starts before you press record. This part covers your tools, lighting, sound, and the hook that stops the scroll.",
          de: "Guter Content beginnt vor der Aufnahme. Dieser Teil deckt Werkzeuge, Licht, Ton und den Hook ab, der das Scrollen stoppt.",
          id: "Konten bagus dimulai sebelum kamu menekan rekam. Bagian ini membahas peralatan, pencahayaan, suara, dan hook yang menghentikan scroll.",
        },
        outcomes: {
          en: ["The kit you need to film cleanly", "How to light and stage a clean kitchen shot", "How to capture sound and hook viewers fast"],
          de: ["Das Equipment f\u00FCr saubere Aufnahmen", "Wie du eine saubere K\u00FCchenaufnahme beleuchtest und inszenierst", "Wie du Ton einf\u00E4ngst und Zuschauer schnell fesselst"],
          id: ["Peralatan yang kamu butuhkan untuk syuting bersih", "Cara mencahayai dan menata shot dapur yang bersih", "Cara menangkap suara dan menarik penonton dengan cepat"],
        },
        lessons: [
          _lesson("m2-2-l1", "carousel", { en: "Sharpen your tools", de: "Werkzeuge schärfen", id: "Asah peralatanmu" }, { cards: 5 }),
          _lesson("m2-2-l2", "video", { en: "Plate the shot & prep like a pro", de: "Den Shot anrichten & profihaft vorbereiten", id: "Tata shot & prep seperti pro" }, { dur: 90 }),
          _lesson("m2-2-l3", "video", { en: "Clean kitchen & set the stage", de: "Saubere Küche & Bühne bereiten", id: "Dapur bersih & siapkan panggung" }, { dur: 60 }),
          _lesson("m2-2-l4", "video", { en: "Light it right", de: "Richtig beleuchten", id: "Cahayai dengan benar" }, { dur: 60 }),
          _lesson("m2-2-l5", "video", { en: "Season your sound", de: "Würze deinen Sound", id: "Bumbui suaramu" }, { dur: 60 }),
          _lesson("m2-2-l6", "video", { en: "Your first bite matters: Hooks", de: "Der erste Bissen zählt: Hooks", id: "Gigitan pertama penting: Hooks" }, { dur: 90 }),
          _lesson("m2-2-l7", "video", { en: "Edit like a pro", de: "Schneiden wie ein Profi", id: "Edit seperti pro" }, { dur: 180 }),
        ],
      },
      {
        id: "m2-3",
        num: "2.3",
        thumb: "https://images.unsplash.com/photo-1466637574441-749b8f19452f?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
        title: { en: "Do's and don'ts of product integration", de: "Do's und Don'ts der Produktintegration", id: "Do's dan don'ts integrasi produk" },
        intro: {
          en: "Featuring UFS products well is an art. This part shows what lands as authentic and what reads as a forced ad.",
          de: "UFS-Produkte gut einzubinden ist eine Kunst. Dieser Teil zeigt, was authentisch wirkt und was wie erzwungene Werbung aussieht.",
          id: "Menampilkan produk UFS dengan baik adalah seni. Bagian ini menunjukkan apa yang terasa autentik dan apa yang terlihat seperti iklan paksaan.",
        },
        outcomes: {
          en: ["How to show products naturally in real cooking", "The dos that keep content authentic", "The don'ts that make content feel like an ad"],
          de: ["Wie du Produkte nat\u00FCrlich beim echten Kochen zeigst", "Die Dos, die Content authentisch halten", "Die Don'ts, die Content wie Werbung wirken lassen"],
          id: ["Cara menampilkan produk secara natural saat memasak", "Do yang menjaga konten tetap autentik", "Don't yang membuat konten terasa seperti iklan"],
        },
        lessons: [
          _lesson("m2-3-l1", "video", { en: "Do's and don'ts of product integration", de: "Do's und Don'ts der Produktintegration", id: "Do's dan don'ts integrasi produk" }, { dur: 90 }),
        ],
      },
    ],
  },
  {
    id: "m3",
    num: "3",
    thumb: "https://images.unsplash.com/photo-1577219491135-ce391730fb2c?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
    title: { en: "Chef to personal brand", de: "Vom Koch zur eigenen Marke", id: "Dari chef ke brand pribadi" },
    intro: {
      en: "What good content looks like, and how to turn consistent posting into a personal brand chefs and audiences recognise.",
      de: "Wie guter Content aussieht und wie du aus konsequentem Posten eine eigene Marke machst, die Köche und Publikum erkennen.",
      id: "Seperti apa konten yang baik, dan cara mengubah posting konsisten menjadi brand pribadi yang dikenali chef dan audiens.",
    },
    outcomes: {
      en: ["What separates good chef content from forgettable posts", "How consistency builds a recognisable personal brand", "How to find a voice chefs and audiences remember"],
      de: ["Was guten Chef-Content von vergessbaren Posts unterscheidet", "Wie Konsistenz eine erkennbare eigene Marke aufbaut", "Wie du eine Stimme findest, die Chefs und Publikum erinnern"],
      id: ["Yang membedakan konten chef bagus dari posting biasa", "Bagaimana konsistensi membangun brand pribadi yang dikenali", "Cara menemukan suara yang diingat chef dan audiens"],
    },
    directLessons: [
      _lesson("m3-l1", "video", { en: "What good content looks like", de: "Wie guter Content aussieht", id: "Seperti apa konten yang baik" }, { dur: 300 }),
    ],
    subModules: [],
  },
  {
    id: "m4",
    num: "4",
    thumb: "https://images.unsplash.com/photo-1414235077428-338989a2e8c0?auto=format&fit=crop&w=1200&h=700&q=70&crop=entropy",
    title: { en: "Interpreting & executing UFS briefs", de: "UFS-Briefings verstehen & umsetzen", id: "Memahami & menjalankan brief UFS" },
    intro: {
      en: "How to read a UFS brief, interpret it for your audience and market, and deliver content that meets the campaign goals.",
      de: "Wie du ein UFS-Briefing liest, für dein Publikum und deinen Markt interpretierst und Content lieferst, der die Kampagnenziele erfüllt.",
      id: "Cara membaca brief UFS, menafsirkannya untuk audiens dan pasarmu, dan menyajikan konten yang memenuhi tujuan kampanye.",
    },
    outcomes: {
      en: ["How to read and interpret a UFS brief", "How to adapt a brief for your market and audience", "How to deliver content that meets campaign goals"],
      de: ["Wie du ein UFS-Briefing liest und interpretierst", "Wie du ein Briefing f\u00FCr deinen Markt anpasst", "Wie du Content lieferst, der Kampagnenziele erf\u00FCllt"],
      id: ["Cara membaca dan menafsirkan brief UFS", "Cara menyesuaikan brief untuk pasar dan audiensmu", "Cara menyajikan konten yang memenuhi tujuan kampanye"],
    },
    directLessons: [
      _lesson("m4-l1", "video", { en: "Interpreting & executing UFS briefs", de: "UFS-Briefings verstehen & umsetzen", id: "Memahami & menjalankan brief UFS" }, { dur: 300 }),
    ],
    subModules: [],
  },
];

// Flatten a module's lessons (direct + all sub-module lessons) in display order.
// Returns [{ lesson, subModule|null, subIdx, lessonIdx, flatIdx }]
function flattenModuleLessons(mod) {
  const out = [];
  let flat = 0;
  (mod.directLessons || []).forEach((lesson, li) => {
    out.push({ lesson, subModule: null, subIdx: -1, lessonIdx: li, flatIdx: flat++ });
  });
  (mod.subModules || []).forEach((sm, si) => {
    (sm.lessons || []).forEach((lesson, li) => {
      out.push({ lesson, subModule: sm, subIdx: si, lessonIdx: li, flatIdx: flat++ });
    });
  });
  return out;
}

function moduleLessonCount(mod) {
  let n = (mod.directLessons || []).length;
  (mod.subModules || []).forEach(sm => { n += (sm.lessons || []).length; });
  return n;
}

function loadCurriculum() {
  return Promise.resolve(CURRICULUM);
}

window.CURRICULUM = CURRICULUM;
window.flattenModuleLessons = flattenModuleLessons;
window.moduleLessonCount = moduleLessonCount;
window.loadCurriculum = loadCurriculum;
window.LESSON_COMPLETION = LESSON_COMPLETION;
