import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  createSafetyQuizResult,
  listSafetyQuizResults,
} from '@/shared/api/safetyQuizResults';
import { apiRequest } from '@/shared/api/httpClient';
import type { SafetyQuizResultCreateDto } from '@/shared/types/safetyQuiz';

vi.mock('@/shared/api/httpClient', () => ({
  apiRequest: vi.fn(),
}));

const apiRequestMock = vi.mocked(apiRequest);

describe('safety quiz results API', () => {
  beforeEach(() => {
    apiRequestMock.mockReset();
  });

  it('lists safety quiz results without query parameters by default', () => {
    void listSafetyQuizResults();

    expect(apiRequestMock).toHaveBeenCalledWith('/safety_quiz_results');
  });

  it('lists safety quiz results filtered by week', () => {
    void listSafetyQuizResults('2026-06-08');

    expect(apiRequestMock).toHaveBeenCalledWith('/safety_quiz_results?week_of=2026-06-08');
  });

  it('creates safety quiz results with POST body wrapped in data', () => {
    const request: SafetyQuizResultCreateDto = {
      quiz_id: 'quiz-1',
      quiz_title: 'Safety Protocols',
      week_of: '2026-06-08',
      score: 4,
      total_questions: 5,
      answers: [0, 1, 2, 1, 0],
    };

    void createSafetyQuizResult(request);

    expect(apiRequestMock).toHaveBeenCalledWith('/safety_quiz_results', {
      method: 'POST',
      body: { data: request },
    });
  });
});
