/* === PHYSL 210 — course schedule / pacing timeline === */

// Real "today" (local time, normalized to noon to avoid DST / time-of-day off-by-one) so every
// countdown ("N days away") and done/now/upcoming status tracks the actual date and updates day to day.
const SCH_TODAY = (() => { const d = new Date(); d.setHours(12, 0, 0, 0); return d; })();
const schD = (s) => new Date(s + 'T12:00:00');
const SCH_MON = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

function schRange(s, e) {
  const a = schD(s);
  if (!e || e === s) return `${SCH_MON[a.getMonth()]} ${a.getDate()}`;
  const b = schD(e);
  return a.getMonth() === b.getMonth()
    ? `${SCH_MON[a.getMonth()]} ${a.getDate()}–${b.getDate()}`
    : `${SCH_MON[a.getMonth()]} ${a.getDate()} – ${SCH_MON[b.getMonth()]} ${b.getDate()}`;
}
function schDaysUntil(s) { return Math.ceil((schD(s) - SCH_TODAY) / 86400000); }
function schStatus(it) {
  const start = schD(it.start || it.date), end = schD(it.end || it.date);
  if (end < SCH_TODAY) return 'done';
  if (start <= SCH_TODAY && SCH_TODAY <= end) return 'now';
  return 'upcoming';
}

const SCHEDULE = [
  { term:'Spring Term', sub:'May – June 2026', items:[
    { kind:'release', date:'2026-05-04', title:'Cell, Blood & Nerve/Muscle/Synapse released' },
    { kind:'study', start:'2026-05-04', end:'2026-05-06', mod:'cell',  lectures:3 },
    { kind:'study', start:'2026-05-07', end:'2026-05-15', mod:'blood', lectures:7 },
    { kind:'holiday', date:'2026-05-18', title:'Victoria Day' },
    { kind:'study', start:'2026-05-19', end:'2026-05-27', mod:'nms',   lectures:7 },
    { kind:'exam', start:'2026-05-28', end:'2026-06-01', title:'1st Mid-term Exam', weight:'15%', detail:'Cell · Blood · NMS' },
    { kind:'release', date:'2026-05-28', title:'CNS, ANS, Special Senses & Cardiovascular released' },
    { kind:'study', start:'2026-06-01', end:'2026-06-05', mod:'cns',   lectures:5 },
    { kind:'study', start:'2026-06-08', end:'2026-06-09', mod:'ans',   lectures:2 },
    { kind:'study', start:'2026-06-10', end:'2026-06-12', mod:'ss',    lectures:3 },
    { kind:'study', start:'2026-06-15', end:'2026-06-23', mod:'cardio',lectures:7 },
    { kind:'exam', start:'2026-06-25', end:'2026-06-26', title:'Spring Term Exam', weight:'35%', detail:'Cumulative · Cell → Cardiovascular' },
  ]},
  { term:'Summer Term', sub:'June – August 2026', items:[
    { kind:'release', date:'2026-06-25', title:'GI & Respiratory released' },
    { kind:'study', start:'2026-06-29', end:'2026-07-08', mod:'gi',    lectures:7 },
    { kind:'holiday', date:'2026-07-01', title:'Canada Day' },
    { kind:'study', start:'2026-07-09', end:'2026-07-17', mod:'resp',  lectures:7 },
    { kind:'exam', start:'2026-07-20', end:'2026-07-21', title:'2nd Mid-term Exam', weight:'15%', detail:'GI · Respiratory' },
    { kind:'release', date:'2026-07-20', title:'Renal, Reproduction & Endocrine released' },
    { kind:'study', start:'2026-07-22', end:'2026-07-30', mod:'renal', lectures:7 },
    { kind:'study', start:'2026-07-31', end:'2026-08-11', mod:'repro', lectures:7 },
    { kind:'holiday', date:'2026-08-03', title:'Heritage Day' },
    { kind:'study', start:'2026-08-12', end:'2026-08-20', mod:'endo',  lectures:7 },
    { kind:'exam', start:'2026-08-24', end:'2026-08-25', title:'Summer Term Exam', weight:'35%', detail:'Cumulative · GI → Endocrine' },
  ]},
];

// first not-yet-finished study block = current focus; first not-done exam = next exam
const SCH_ALL = SCHEDULE.flatMap(t => t.items);
const SCH_FOCUS = (SCH_ALL.find(i => i.kind === 'study' && schStatus(i) !== 'done') || {}).mod;
const SCH_NEXT_EXAM = SCH_ALL.find(i => i.kind === 'exam' && schStatus(i) !== 'done');

Object.assign(window, { SCHEDULE, schRange, schDaysUntil, schStatus, SCH_FOCUS, SCH_NEXT_EXAM });
