import { describe, expect, it } from 'vitest';
import {
  toWalkthroughCheckinCreateDto,
  type WalkthroughCommentDraft,
  type WalkthroughRatingDraft,
} from '@/business/walkthrough/mappers';
import { WALKTHROUGH_TEST_SEED } from '@/test-seeds/walkthrough';

describe('walkthrough mappers', () => {
  it('maps a completed walkthrough draft into the backend create DTO shape', () => {
    const ratings: WalkthroughRatingDraft = {
      attitude: 4,
      classroom_management: 3,
      cleanliness: 4,
      vibes: 3,
      team_dynamics: 4,
      emergency_exit: 3,
      lesson_plan: 2,
    };
    const comments: WalkthroughCommentDraft = {
      attitude: '  Warm arrival routine  ',
      classroom_management: '',
      cleanliness: 'Materials labeled',
      vibes: undefined,
      team_dynamics: '  Strong para coordination  ',
      emergency_exit: 'Door kit visible',
      lesson_plan: '  Needs posting  ',
    };

    expect(
      toWalkthroughCheckinCreateDto({
        teacherName: WALKTHROUGH_TEST_SEED.teacherName,
        classroom: WALKTHROUGH_TEST_SEED.classroom,
        directorName: WALKTHROUGH_TEST_SEED.directorName,
        date: '2026-06-08',
        time: '09:15',
        ratings,
        comments,
        overallNotes: '  Follow up on lesson plan posting.  ',
      }),
    ).toEqual({
      teacher_name: WALKTHROUGH_TEST_SEED.teacherName,
      classroom: WALKTHROUGH_TEST_SEED.classroom,
      director_name: WALKTHROUGH_TEST_SEED.directorName,
      check_in_date: '2026-06-08',
      check_in_time: '09:15',
      attitude_rating: 4,
      attitude_comment: 'Warm arrival routine',
      classroom_management_rating: 3,
      classroom_management_comment: undefined,
      cleanliness_rating: 4,
      cleanliness_comment: 'Materials labeled',
      vibes_rating: 3,
      vibes_comment: undefined,
      team_dynamics_rating: 4,
      team_dynamics_comment: 'Strong para coordination',
      emergency_exit_rating: 3,
      emergency_exit_comment: 'Door kit visible',
      lesson_plan_rating: 2,
      lesson_plan_comment: 'Needs posting',
      overall_notes: 'Follow up on lesson plan posting.',
    });
  });

  it('omits blank optional text fields from the backend create DTO shape', () => {
    const ratings: WalkthroughRatingDraft = {
      attitude: 4,
      classroom_management: 4,
      cleanliness: 4,
      vibes: 4,
      team_dynamics: 4,
      emergency_exit: 3,
      lesson_plan: 3,
    };

    expect(
      toWalkthroughCheckinCreateDto({
        teacherName: WALKTHROUGH_TEST_SEED.teacherName,
        classroom: WALKTHROUGH_TEST_SEED.classroom,
        directorName: WALKTHROUGH_TEST_SEED.directorName,
        date: '2026-06-08',
        time: '10:30',
        ratings,
        comments: {},
        overallNotes: '   ',
      }),
    ).toMatchObject({
      attitude_comment: undefined,
      classroom_management_comment: undefined,
      cleanliness_comment: undefined,
      vibes_comment: undefined,
      team_dynamics_comment: undefined,
      emergency_exit_comment: undefined,
      lesson_plan_comment: undefined,
      overall_notes: undefined,
    });
  });

  it('fails explicitly when a required walkthrough rating is missing', () => {
    const ratings: WalkthroughRatingDraft = {
      attitude: 4,
      classroom_management: 4,
      cleanliness: 4,
      vibes: 4,
      team_dynamics: 4,
      emergency_exit: 3,
    };

    expect(() =>
      toWalkthroughCheckinCreateDto({
        teacherName: WALKTHROUGH_TEST_SEED.teacherName,
        classroom: WALKTHROUGH_TEST_SEED.classroom,
        directorName: WALKTHROUGH_TEST_SEED.directorName,
        date: '2026-06-08',
        time: '11:00',
        ratings,
        comments: {},
        overallNotes: '',
      }),
    ).toThrow('Missing walkthrough rating for lesson_plan');
  });
});
