import { describe, expect, it } from 'vitest';
import {
  toPersonalityQuizResultMutationDto,
  toPersonalityQuizResultViewModel,
} from '@/business/personality/mappers';
import type { PersonalityQuizSubmission } from '@/business/personality/types';
import type { PersonalityQuizResultDto } from '@/shared/types/personality';
import { PERSONALITY_QUIZ_KINDS } from '@/shared/constants/personality';

describe('personality mappers', () => {
  it('maps a null backend result to a null view model', () => {
    expect(toPersonalityQuizResultViewModel(null)).toBeNull();
  });

  it('maps backend quiz result DTO fields into the frontend view model shape', () => {
    const dto: PersonalityQuizResultDto = {
      id: 'personality-1',
      quiz_kind: PERSONALITY_QUIZ_KINDS.personalityType,
      quiz_id: 'personality-type',
      quiz_title: 'Personality Type Quiz',
      personality_type: 'INFJ',
      quiz_answers: {
        '1': 'I',
        '2': 'N',
        '3': 'F',
        '4': 'J',
      },
      score: null,
      total_questions: 4,
      result_label: 'INFJ',
      result_payload: null,
      week_of: null,
      user_name: 'Emily Johnson',
      user_role: 'teacher',
      completed_at: '2026-06-08T08:00:00.000Z',
      organizationId: 'org-1',
      campusId: 'campus-1',
      userId: 'user-1',
      createdById: 'user-1',
      updatedById: null,
      createdAt: '2026-06-08T08:00:00.000Z',
      updatedAt: '2026-06-08T09:00:00.000Z',
    };

    expect(toPersonalityQuizResultViewModel(dto)).toEqual({
      quizKind: PERSONALITY_QUIZ_KINDS.personalityType,
      quizId: 'personality-type',
      quizTitle: 'Personality Type Quiz',
      personalityType: 'INFJ',
      quizAnswers: {
        1: 'I',
        2: 'N',
        3: 'F',
        4: 'J',
      },
      score: null,
      totalQuestions: 4,
      resultLabel: 'INFJ',
      resultPayload: null,
      weekOf: null,
      completedAt: '2026-06-08T08:00:00.000Z',
      updatedAt: '2026-06-08T09:00:00.000Z',
    });
  });

  it('maps quiz submission state back into the backend mutation DTO shape', () => {
    const submission: PersonalityQuizSubmission = {
      quizKind: PERSONALITY_QUIZ_KINDS.personalityType,
      quizId: 'personality-type',
      quizTitle: 'Personality Type Quiz',
      personalityType: 'ESTP',
      quizAnswers: {
        1: 'E',
        2: 'S',
        3: 'T',
        4: 'P',
      },
      totalQuestions: 4,
    };

    expect(toPersonalityQuizResultMutationDto(submission)).toEqual({
      quiz_kind: PERSONALITY_QUIZ_KINDS.personalityType,
      quiz_id: 'personality-type',
      quiz_title: 'Personality Type Quiz',
      personality_type: 'ESTP',
      quiz_answers: {
        '1': 'E',
        '2': 'S',
        '3': 'T',
        '4': 'P',
      },
      score: null,
      total_questions: 4,
      result_label: 'ESTP',
      result_payload: undefined,
    });
  });
});
