import { describe, expect, it } from 'vitest';

import {
  parseSafetyQuizPayload,
  serializeSafetyQuizPayload,
} from '@/business/safety-quiz/selectors';
import type { SafetyQuiz } from '@/shared/types/app';

const quiz: SafetyQuiz = {
  id: 'qbs-weekly',
  title: 'Weekly Safety Quiz',
  focus: 'de-escalation',
  weeklyFocus: {
    title: 'De-escalation',
    description: 'Practice calm responses.',
  },
  keyReminders: ['Use calm voice'],
  questions: [
    {
      id: 'q1',
      question: 'What comes first?',
      options: ['Reduce demands', 'Raise voice'],
      correctIndex: 0,
      explanation: 'Reducing demands helps prevent escalation.',
    },
  ],
};

describe('safety quiz selectors', () => {
  it('serializes and parses editable safety quiz payloads', () => {
    expect(parseSafetyQuizPayload(serializeSafetyQuizPayload(quiz))).toEqual(quiz);
  });

  it('rejects incomplete editable safety quiz payloads', () => {
    expect(parseSafetyQuizPayload(JSON.stringify({ ...quiz, questions: [] }))).toBe('Questions must be a non-empty array.');
    expect(parseSafetyQuizPayload(JSON.stringify({ ...quiz, focus: 'invalid' }))).toBe(
      'Safety quiz focus must be physical-management, de-escalation, or safety-reminders.',
    );
  });
});
