/* ENSSA Reporting — vocabulary tokens.
   Aligned to the locked design brief + actual task battery spec (Mike, Apr 2026).
   Risk language: deficit-free throughout.
   Forbidden: theta, SE, posterior, residual, subscore, subtest, probe,
     "above/below expectation", "as expected".
*/

/* ── Risk bands ── */
const RISK = {
  low:       { id: 'low',       label: 'Low risk',                color: 'var(--risk-on-track)', bg: 'var(--risk-on-track-bg)', fg: 'var(--risk-on-track-fg)' },
  moderate:  { id: 'moderate',  label: 'Moderate risk',           color: 'var(--risk-emerging)', bg: 'var(--risk-emerging-bg)', fg: 'var(--risk-emerging-fg)' },
  high:      { id: 'high',      label: 'High risk',               color: 'var(--risk-concern)',  bg: 'var(--risk-concern-bg)',  fg: 'var(--risk-concern-fg)' },
  more_info: { id: 'more_info', label: 'Unknown',                  color: 'var(--ink-400)',       bg: 'var(--ink-100)',          fg: 'var(--fg-2)' },
};
const RISK_ORDER = ['low', 'moderate', 'high', 'more_info'];

const RISK_COPY = {
  low:       { headline: 'Likely on track for this point in the year.',           detail: 'Continue Tier 1 instruction.' },
  moderate:  { headline: 'May benefit from additional support.',                   detail: 'Monitor closely; consider small-group teaching.' },
  high:      { headline: 'May benefit from targeted support.',                     detail: 'Plan structured small-group or individual support.' },
  more_info: { headline: 'Unknown — confirm task completion before drawing a conclusion.', detail: 'Confirm task completion or re-administer affected tasks.' },
};

/* ── Screening completion ── */
const STATUS = {
  completed:  { id: 'completed',  label: 'Completed',   color: 'var(--risk-on-track)' },
  partial:    { id: 'partial',    label: 'Partial',     color: 'var(--risk-emerging)' },
  notstarted: { id: 'notstarted', label: 'Not started', color: 'var(--ink-300)' },
};

/* ── Teaching notes ── */
const TEACHING_NOTE = {
  priority:     { id: 'priority',     label: 'Priority focus area',     tone: 'concern' },
  needs:        { id: 'needs',        label: 'May benefit from support', tone: 'emerging' },
  monitor:      { id: 'monitor',      label: 'Worth monitoring',        tone: 'emerging' },
  insufficient: { id: 'insufficient', label: 'Unknown',                  tone: 'neutral' },
  none:         { id: 'none',         label: '',                        tone: 'neutral' },
};

/* ── Term windows (T2 is not a screening window) ── */
const TERMS = [
  { id: '2026-T1', label: 'Term 1, 2026', t: 'T1' },
  { id: '2025-T4', label: 'Term 4, 2025', t: 'T4' },
  { id: '2025-T3', label: 'Term 3, 2025', t: 'T3' },
  { id: '2025-T1', label: 'Term 1, 2025', t: 'T1' },
];
const TERM_DEFAULT = '2025-T4';

const YEAR_LEVELS = [
  { id: 'F',  label: 'Foundation' },
  { id: 'Y1', label: 'Year 1' },
];

/* ─────────────────────────────────────────────────────────────
   TASK BATTERY SPEC
   Source: Mike's notes, Apr 2026.
   • STPM and STDD excluded (non-numeracy).
   • Decomposition V1 (T1) and V2 (T3/T4) shown with same label —
     different item IDs, same construct/scale, never co-occur.
   • AADD/ASDD (drag-drop) and AAMC/ASMC (multi-choice) are distinct.
   • 5–7 tasks per battery; layout must handle that range.
   ───────────────────────────────────────────────────────────── */

/* All possible task definitions (superset across all batteries) */
const TASK_DEFS = {
  /* Foundation shared */
  mc20:    { id: 'mc20',    label: 'Magnitude comparison',            detail: '0–20' },
  bnl20:   { id: 'bnl20',   label: 'Bounded number line',            detail: '0–20' },
  /* Foundation cohort-A specific */
  mnc20:   { id: 'mnc20',   label: 'Missing number',                  detail: '0–20' },
  mq10:    { id: 'mq10',    label: 'Match quantity',                  detail: '1–10' },
  unlnc20: { id: 'unlnc20', label: 'Unbounded number line',           detail: '0–20' },
  dmt5:    { id: 'dmt5',    label: 'Decomposition',                   detail: '5' },
  /* Foundation cohort-B specific */
  mna20:   { id: 'mna20',   label: 'Missing number',                  detail: '0–20' },
  mq20:    { id: 'mq20',    label: 'Match quantity',                  detail: '1–20' },
  unlc20:  { id: 'unlc20',  label: 'Unbounded number line',           detail: '0–20' },
  dmt10:   { id: 'dmt10',   label: 'Decomposition',                  detail: '10' },
  /* Year 1 shared */
  mc100:   { id: 'mc100',   label: 'Magnitude comparison',           detail: '0–100' },
  bnl100:  { id: 'bnl100',  label: 'Bounded number line',            detail: '0–100' },
  /* Year 1 cohort-A specific */
  mna100:  { id: 'mna100',  label: 'Missing number',                  detail: '0–100' },
  unlnc20y1:{ id: 'unlnc20y1', label: 'Unbounded number line',       detail: '0–20' },
  /* Year 1 cohort-B specific */
  mnc100:  { id: 'mnc100',  label: 'Missing number',                  detail: '0–100' },
  unlc20y1:{ id: 'unlc20y1', label: 'Unbounded number line',          detail: '0–20' },
  /* Year 1 arithmetic (format varies by term) */
  aadd:    { id: 'aadd',    label: 'Addition',                       detail: 'drag-drop' },
  asdd:    { id: 'asdd',    label: 'Subtraction',                    detail: 'drag-drop' },
  aamc:    { id: 'aamc',    label: 'Addition',                       detail: 'multi-choice' },
  asmc:    { id: 'asmc',    label: 'Subtraction',                    detail: 'multi-choice' },
};

/*
  Battery lookup: TASK_BATTERIES[year][cohort][termCode] → array of task IDs.
  termCode: 'T1' | 'T3' | 'T4'
  Cohorts: 'A' (Foundation-A / Year1-A) or 'B' (Foundation-B / Year1-B)
*/
const TASK_BATTERIES = {
  F: {
    A: {
      T1: ['mc20','mnc20','mq10','bnl20','unlnc20','dmt5'],
      T3: ['mc20','mnc20','mq10','bnl20','unlnc20','dmt5'],   // dmt5 V2
      T4: ['mc20','mnc20','mq10','bnl20','unlnc20','dmt5'],   // dmt5 V2
    },
    B: {
      T1: ['mc20','mna20','mq20','bnl20','unlc20','dmt10'],
      T3: ['mc20','mna20','mq20','bnl20','dmt10'],            // ⚠️ no UNL in F-B T3
      T4: ['mc20','mna20','mq20','bnl20','unlc20','dmt10'],
    },
  },
  Y1: {
    A: {
      T1: ['mc100','mna100','bnl100','unlnc20y1','aadd','asdd'],
      T3: ['mc100','mna100','bnl100','unlnc20y1','aamc','asdd','asmc'],
      T4: ['mc100','mna100','bnl100','unlnc20y1','aamc','asmc'],
    },
    B: {
      T1: ['mc100','mnc100','bnl100','unlc20y1','aadd','asdd'],
      T3: ['mc100','mnc100','bnl100','unlc20y1','aamc','asdd','asmc'],
      T4: ['mc100','mnc100','bnl100','unlc20y1','aamc','asmc'],
    },
  },
};

/* Helper: get the battery for a given (year, cohort, termId) */
function getBattery(year, cohort, termId) {
  const termObj = TERMS.find(t => t.id === termId) || TERMS[0];
  const t = termObj.t; // 'T1' | 'T3' | 'T4'
  const batteries = (TASK_BATTERIES[year] || {})[cohort] || {};
  const ids = batteries[t] || batteries['T1'] || [];
  return ids.map(id => TASK_DEFS[id]).filter(Boolean);
}

/* Legacy TASKS list (T1, Year 1-A — used as fallback for aggregate charts) */
const TASKS = getBattery('Y1', 'A', '2026-T1');

Object.assign(window, {
  RISK, RISK_ORDER, RISK_COPY, STATUS, TASKS, TASK_DEFS, TASK_BATTERIES,
  TEACHING_NOTE, TERMS, TERM_DEFAULT, YEAR_LEVELS, getBattery,
});
