import { useSchoolSettingsStore } from '../store/schoolSettings';

/**
 * Education-level-aware terminology resolver (feature/shapeshift).
 *
 * The terms are derived from the school's education level setting
 * (primary, secondary, high_school, tertiary) as described in
 * schoolSettings.ts.
 *
 * Returned keys:
 *   subject     – "Subject" (P/S/HS) or "Course" (Tertiary)
 *   subjects    – plural form
 *   chapter     – "Chapter" or "Module"
 *   chapters    – plural
 *   classroom   – "Class Room" or "Course Room"
 *   programme   – "Form" / "Class" (P/S/HS) or "Programme" (Tertiary)
 *   grade       – "Grade" (Primary) or "Form / Class" otherwise
 *   unit        – generic shapeshift-friendly alias for subject/course
 */
export interface EducationTerms {
  subject: string;
  subjects: string;
  chapter: string;
  chapters: string;
  classroom: string;
  programme: string;
  grade: string;
  unit: string;
  units: string;
}

type Level = 'primary' | 'secondary' | 'high_school' | 'tertiary';

const TERM_MAP: Record<Level, EducationTerms> = {
  primary: {
    subject: 'Subject',
    subjects: 'Subjects',
    chapter: 'Chapter',
    chapters: 'Chapters',
    classroom: 'Class Room',
    programme: 'Grade',
    grade: 'Grade',
    unit: 'Subject',
    units: 'Subjects',
  },
  secondary: {
    subject: 'Subject',
    subjects: 'Subjects',
    chapter: 'Chapter',
    chapters: 'Chapters',
    classroom: 'Class Room',
    programme: 'Form',
    grade: 'Form',
    unit: 'Subject',
    units: 'Subjects',
  },
  high_school: {
    subject: 'Subject',
    subjects: 'Subjects',
    chapter: 'Chapter',
    chapters: 'Chapters',
    classroom: 'Class Room',
    programme: 'Form',
    grade: 'Form',
    unit: 'Subject',
    units: 'Subjects',
  },
  tertiary: {
    subject: 'Course',
    subjects: 'Courses',
    chapter: 'Module',
    chapters: 'Modules',
    classroom: 'Course Room',
    programme: 'Programme',
    grade: 'Year',
    unit: 'Course',
    units: 'Courses',
  },
};

const DEFAULT_TERMS: EducationTerms = TERM_MAP.secondary;

export function useEducationTerms(): EducationTerms {
  const educationLevel = useSchoolSettingsStore((s) => s.educationLevel);
  return (TERM_MAP[educationLevel as Level] ?? DEFAULT_TERMS);
}

/**
 * Pure (non-reactive) lookup for callers that already hold the level
 * (e.g. server-side label builders, tests, non-component code paths).
 */
export function getEducationTerms(level: string | null | undefined): EducationTerms {
  return (TERM_MAP[level as Level] ?? DEFAULT_TERMS);
}