import { apiRequest } from '@/shared/api/httpClient';
import { ApiListResponse } from '@/shared/types/api';
import {
  SafetyQuizResultCreateDto,
  SafetyQuizResultDto,
} from '@/shared/types/safetyQuiz';

const SAFETY_QUIZ_RESULTS_PATH = '/safety_quiz_results';

function createSafetyQuizResultsPath(weekOf?: string): string {
  if (!weekOf) {
    return SAFETY_QUIZ_RESULTS_PATH;
  }

  return `${SAFETY_QUIZ_RESULTS_PATH}?${new URLSearchParams({ week_of: weekOf }).toString()}`;
}

export function listSafetyQuizResults(weekOf?: string): Promise<ApiListResponse<SafetyQuizResultDto>> {
  return apiRequest<ApiListResponse<SafetyQuizResultDto>>(createSafetyQuizResultsPath(weekOf));
}

export function createSafetyQuizResult(request: SafetyQuizResultCreateDto): Promise<SafetyQuizResultDto> {
  return apiRequest<SafetyQuizResultDto>(SAFETY_QUIZ_RESULTS_PATH, {
    method: 'POST',
    body: { data: request },
  });
}
