// Unit tests for the terminology dictionary + useTerminology hook.
//
// These exercise the PDF §5/§6/§8 mappings and assert the shape of the
// dictionary so accidental string rotations get caught.

import {
  T,
  T_TIERED,
  TIER,
  ACTIVE_TIER,
  termFor,
  useTerminology,
} from '../lib/terminology';

describe('T (flat terminology dictionary)', () => {
  it('replaces Student with Learner globally (PDF §5)', () => {
    expect(T.STUDENT).toBe('Learner');
    expect(T.STUDENTS).toBe('Learners');
  });

  it('carries PDF §8 curriculum-portfolio replacements', () => {
    expect(T.MATERIAL_ASSETS).toBe('Learning Material');
    expect(T.VIEW_ROSTER).toBe('View Learners');
    expect(T.ARCHIVE_COURSE).toBe('Delete');
  });

  it('carries PDF §6 teacher-dashboard replacements', () => {
    expect(T.GRADING).toBe('Marking');
    expect(T.THRESHOLD).toBe('Average');
    expect(T.MARKED).toBe('Excused');
    expect(T.OUTLIERS).toBe('Below Average');
    expect(T.STUDENT_DRILLDOWN).toBe('Learner Remedial Record');
  });

  it('carries PDF §9 academic-structure replacements', () => {
    expect(T.PAGE_TITLE_COURSE_SCHEME).toBe('Class Room');
    expect(T.EXPORT_INTEL).toBe('Export Scheme');
    expect(T.NEW_SECTION).toBe('New Assignment');
    expect(T.SEARCH_CHAPTER_MODULE).toBe('Search Chapter/Module');
    expect(T.ADD_SUBJECT).toBe('Add Learning Material');
    expect(T.CLASS_SECTIONS).toBe('Course Scheme/Plan');
    expect(T.CURRICULUM_LEDGER).toBe('Learning Material Shortcut');
  });

  it('carries PDF §7 notice-board replacement', () => {
    expect(T.BROADCAST).toBe('Notification');
  });

  it('carries the §6 follow-up teacher-dashboard strings', () => {
    expect(T.BELOW_THRESHOLD_HEADER).toBe('Below Average');
    expect(T.BELOW_THRESHOLD_EMPTY).toBe('No learners below the 50% average. 🎉');
    expect(T.OUTLIERS_HEADING).toBe('Below Average (avg < 50%)');
    expect(T.OUTLIERS_CTA).toBe('Click a learner above');
    expect(T.SELECT_FLAGGED_LEARNER).toBe('Select a flagged learner to see their history.');
  });

  it('carries the §6 follow-up exam-module renames', () => {
    expect(T.GRADING_COLUMN).toBe('Marking');
    expect(T.PASS_THRESHOLD).toBe('Pass Average');
    expect(T.GRADING_SCALE).toBe('Marking Scale');
    expect(T.RECENT_GRADES).toBe('Recent Marks');
  });
});

describe('T_TIERED (tier-aware dictionary)', () => {
  it('maps Course per tier correctly', () => {
    expect(T_TIERED.Course.primary).toBe('Grade');
    expect(T_TIERED.Course.secondary).toBe('Subject');
    expect(T_TIERED.Course.tertiary).toBe('Course');
  });

  it('maps ClassRoom per tier correctly', () => {
    expect(T_TIERED.ClassRoom.primary).toBe('Class Room');
    expect(T_TIERED.ClassRoom.secondary).toBe('Class Room');
    expect(T_TIERED.ClassRoom.tertiary).toBe('Course Room');
  });

  it('maps Chapter/Module per tier correctly', () => {
    expect(T_TIERED.Chapter.primary).toBe('Chapter');
    expect(T_TIERED.Chapter.secondary).toBe('Chapter');
    expect(T_TIERED.Chapter.tertiary).toBe('Module');
  });
});

describe('termFor() and useTerminology()', () => {
  it('termFor returns the entry for the active tier, falling back to tertiary then default', () => {
    expect(termFor({ primary: 'X', tertiary: 'Y' })).toBe('Y');
    expect(termFor({ primary: 'X', default: 'Z' })).toBe('Z');
  });

  it('termFor returns empty string when given a falsy entry', () => {
    expect(termFor(null)).toBe('');
    expect(termFor(undefined)).toBe('');
    expect(termFor({})).toBe('');
  });

  it('useTerminology returns tier, dictionaries, term, and label', () => {
    const api = useTerminology();
    expect(api.tier).toBe(ACTIVE_TIER);
    expect(api.T).toBe(T);
    expect(typeof api.term).toBe('function');
    expect(typeof api.label).toBe('function');
  });

  it('label() resolves a tiered entry to the active tier', () => {
    const { label } = useTerminology();
    expect(label('Course')).toBe('Course');
    expect(label('ClassRoom')).toBe('Course Room');
  });

  it('label() falls back when the key is not a T_TIERED entry', () => {
    const { label } = useTerminology();
    expect(label('Unknown', 'fallback')).toBe('fallback');
    expect(label('Unknown')).toBe('Unknown');
  });
});

describe('TIER / ACTIVE_TIER', () => {
  it('TIER exposes the three educational tiers', () => {
    expect(TIER.PRIMARY).toBe('primary');
    expect(TIER.SECONDARY).toBe('secondary');
    expect(TIER.TERTIARY).toBe('tertiary');
  });

  it('ACTIVE_TIER defaults to tertiary until school_settings.school_tier lands', () => {
    expect(ACTIVE_TIER).toBe('tertiary');
  });
});
