/* EireneEduCloud — app.js (homepage + global helpers)
   Eirene Technologies Limited | Empowering Kenya's Next Generation */
(function (w, d) {
  var EE = w.EE || {};

  function lessonIds(subject) {
    var ids = [];
    subject.units.forEach(function (u) { u.lessons.forEach(function (l) { ids.push(l.id); }); });
    return ids;
  }
  function countLessons(subject) {
    var n = 0; subject.units.forEach(function (u) { n += u.lessons.length; }); return n;
  }

  // ---- Subject grid on the homepage ----
  EE.renderSubjects = function () {
    var grid = d.getElementById('subject-grid');
    if (!grid || !w.CURRICULUM) return;
    var pt = w.ProgressTracker;
    grid.innerHTML = '';
    w.CURRICULUM.subjects.forEach(function (s) {
      var prog = pt ? pt.subjectProgress(lessonIds(s)) : { pct: 0, done: 0, total: countLessons(s) };
      var vids = 0;
      s.units.forEach(function (u) { u.lessons.forEach(function (l) { if (l.video && (typeof l.video === 'string' ? l.video : l.video.id)) vids++; }); });
      var card = d.createElement('a');
      card.className = 'subject-card accent-' + s.color;
      card.href = 'subject.html?lot=' + s.lot;
      card.innerHTML =
        '<div class="sc-top">' +
          '<span class="sc-icon">' + s.icon + '</span>' +
          '<span class="sc-lot">Lot ' + s.lot + '</span>' +
        '</div>' +
        '<h3>' + s.name + '</h3>' +
        '<p class="sc-blurb">' + s.blurb + '</p>' +
        '<div class="sc-meta"><span>' + s.units.length + ' units</span><span>·</span><span>' + prog.total + ' lessons</span><span>·</span><span>🎬 ' + vids + ' videos</span></div>' +
        '<div class="sc-bar"><i style="width:' + prog.pct + '%"></i></div>' +
        '<div class="sc-prog">' + prog.pct + '% complete</div>';
      grid.appendChild(card);
    });
  };

  // ---- Hero stat counters (computed live from the curriculum, never overstated) ----
  EE.renderOverall = function () {
    var c = w.CURRICULUM;
    if (c) {
      var lessons = 0, questions = 0;
      c.subjects.forEach(function (s) {
        s.units.forEach(function (u) {
          u.lessons.forEach(function (l) {
            lessons++;
            if (l.quiz && l.quiz.questions) questions += l.quiz.questions.length;
          });
        });
      });
      var set = function (id, v) { var n = d.getElementById(id); if (n) n.textContent = v; };
      set('ov-subjects', c.subjects.length);
      set('ov-lessons', lessons);
      set('ov-questions', questions);
    }
    var node = d.getElementById('ov-progress');
    if (node && w.ProgressTracker) node.textContent = w.ProgressTracker.overall().pct + '%';
  };

  // ---- Help modal ----
  EE.help = function () {
    var m = d.getElementById('helpModal');
    if (m) m.classList.toggle('open');
  };

  // ---- Toast ----
  EE.toast = function (msg) {
    var t = d.getElementById('ee-toast');
    if (!t) { t = d.createElement('div'); t.id = 'ee-toast'; t.className = 'toast'; d.body.appendChild(t); }
    t.textContent = msg;
    t.classList.add('show');
    if (EE._tt) clearTimeout(EE._tt);
    EE._tt = setTimeout(function () { t.classList.remove('show'); }, 2600);
  };

  // ---- Global keyboard shortcuts ----
  d.addEventListener('keydown', function (e) {
    if (e.key === 'Escape') { var m = d.getElementById('helpModal'); if (m) m.classList.remove('open'); }
    var tag = (e.target && e.target.tagName) || '';
    if (tag === 'INPUT' || tag === 'TEXTAREA') return;
    if (e.key === 'h' || e.key === 'H') EE.help();
  });

  // close modal on backdrop click
  d.addEventListener('click', function (e) {
    var m = d.getElementById('helpModal');
    if (m && m.classList.contains('open') && e.target === m) m.classList.remove('open');
  });

  EE.init = function () {
    EE.renderSubjects();
    EE.renderOverall();
  };

  w.EE = EE;
  if (d.readyState === 'loading') d.addEventListener('DOMContentLoaded', EE.init);
  else EE.init();
})(window, document);
