/**
 * Terminology dictionary for the "Shapeshifting UI" requirement
 * (PDF §5, §6, §8) plus the global substitutions list (PDF §5).
 *
 * Goal: every user-facing label in the app pulls from this dictionary so
 * future tier-aware switching (Primary / Secondary / Tertiary) only has
 * to change the dictionary, not hunt down 100 separate components.
 *
 * Today `TIER` is hard-coded to `'tertiary'` so the demo deployable keeps
 * its existing labels while still routing every UI through the same
 * gateway. A separate P1-UX-5b follow-up will switch the source based on
 * school_settings.school_tier once the settings table exposes that
 * column.
 *
 * Notes:
 *   - Some labels in the PDF (e.g. Chapter in Primary) are tier-deep and
 *     only matter on specific pages. Those stay page-local for now and
 *     get re-exported here as we migrate them.
 *   - Code identifiers (variable names, SQL columns, route paths) are
 *     intentionally NOT touched — the substitution is at the UI layer.
 */

export const TIER = Object.freeze({
  PRIMARY: 'primary',
  SECONDARY: 'secondary',
  TERTIARY: 'tertiary',
});

export const ACTIVE_TIER = TIER.TERTIARY;

export const T = Object.freeze({
  // §5: GLOBAL Student → Learner
  STUDENT: 'Learner',
  STUDENTS: 'Learners',
  STUDENT_ID_LOWER: 'learner',

  // §5: Primary/Secondary tier
  SUBJECT: 'Subject',           // Secondary
  SYLLABUS: 'Syllabus',
  CHAPTER: 'Chapter',
  CLASS_ROOM: 'Class Room',

  // §5: Tertiary tier
  COURSE: 'Course',
  COURSE_OUTLINE: 'Course Outline',
  MODULE: 'Module',
  COURSE_ROOM: 'Course Room',
  LECTURER: 'Lecturer',

  // §8: Curriculum Portfolio
  MATERIAL_ASSETS: 'Learning Material',
  VIEW_ROSTER: 'View Learners',
  ARCHIVE_COURSE: 'Delete',

  // §6: Teacher Dashboard
  GRADING: 'Marking',
  THRESHOLD: 'Average',
  MARKED: 'Excused',
  OUTLIERS: 'Below Average',
  STUDENT_DRILLDOWN: 'Learner Remedial Record',

  // §7: Notice Board
  BROADCAST: 'Notification',

  // §9: Academic Structure / Course Scheme/Plan
  PAGE_TITLE_COURSE_SCHEME: 'Class Room',
  EXPORT_INTEL: 'Export Scheme',
  NEW_SECTION: 'New Assignment',
  SEARCH_CHAPTER_MODULE: 'Search Chapter/Module',
  ADD_SUBJECT: 'Add Learning Material',
  CLASS_SECTIONS: 'Course Scheme/Plan',
  CURRICULUM_LEDGER: 'Learning Material Shortcut',

  // §6 follow-up: per-subject table column + empty-state copy
  // (was "Below 50%" header and "No students below the 50% threshold." text)
  BELOW_THRESHOLD_HEADER: 'Below Average',
  BELOW_THRESHOLD_EMPTY: 'No learners below the 50% average. 🎉',
  // §5 follow-up: outlier panel heading
  OUTLIERS_HEADING: 'Below Average (avg < 50%)',
  OUTLIERS_CTA: 'Click a learner above',

  // §6 follow-up: exam module renames
  GRADING_COLUMN: 'Marking',
  PASS_THRESHOLD: 'Pass Average',
  GRADING_SCALE: 'Marking Scale',

  // §9 follow-up: academic structure "Recent Grades" → "Recent Marks"
  RECENT_GRADES: 'Recent Marks',
  // §9 follow-up: learner profile "flagged" → "flagged" (kept; flagged is a
  // technical term not a UI string), but flag picker copy from §10
  SELECT_FLAGGED_LEARNER: 'Select a flagged learner to see their history.',
});

/**
 * Resolve a label for the currently active tier.
 * Use for entries whose PDF-§5 mapping depends on tier.
 */
export function termFor(map) {
  if (!map || typeof map !== 'object') return '';
  const v = map[ACTIVE_TIER];
  return v === undefined ? map.tertiary ?? map.default ?? '' : v;
}

/**
 * Tier-aware variant of `T` for things that actually differ. Each entry
 * is keyed by tier and yields the label the UI should render.
 */
export const T_TIERED = Object.freeze({
  Subject:    { primary: 'Subject',   secondary: 'Subject',  tertiary: 'Course' },
  Course:     { primary: 'Grade',     secondary: 'Subject',  tertiary: 'Course' },
  ClassRoom:  { primary: 'Class Room',secondary: 'Class Room',tertiary: 'Course Room' },
  Chapter:    { primary: 'Chapter',   secondary: 'Chapter',  tertiary: 'Module' },
  Teacher:    { primary: 'Teacher',   secondary: 'Teacher',  tertiary: 'Lecturer' },
});

export function useTerminology() {
  return {
    tier: ACTIVE_TIER,
    T,
    T_TIERED,
    term: termFor,
    /**
     * Render a tier-aware label. Falls back to a default if the entry
     * has no mapping for the active tier.
     */
    label(key, fallback) {
      const map = T_TIERED[key];
      if (!map) return fallback ?? key;
      const v = map[ACTIVE_TIER];
      return v ?? fallback ?? key;
    },
  };
}

export default useTerminology;
