import { apiRequest } from '@/shared/api/httpClient';
import { ApiListResponse } from '@/shared/types/api';
import type {
  PersonalityDistributionDto,
  PersonalityQuizResultDto,
  PersonalityQuizResultMutationDto,
} from '@/shared/types/personality';

const PERSONALITY_RESULTS_PATH = '/personality_quiz_results';

export function getCurrentPersonalityResult(): Promise<PersonalityQuizResultDto | null> {
  return apiRequest<PersonalityQuizResultDto | null>(`${PERSONALITY_RESULTS_PATH}/me`);
}

export function saveCurrentPersonalityResult(
  request: PersonalityQuizResultMutationDto,
): Promise<PersonalityQuizResultDto> {
  return apiRequest<PersonalityQuizResultDto>(`${PERSONALITY_RESULTS_PATH}/me`, {
    method: 'PUT',
    body: { data: request },
  });
}

export function listPersonalityDistribution(campusId?: string): Promise<ApiListResponse<PersonalityDistributionDto>> {
  if (!campusId) {
    return apiRequest<ApiListResponse<PersonalityDistributionDto>>(`${PERSONALITY_RESULTS_PATH}/distribution`);
  }

  return apiRequest<ApiListResponse<PersonalityDistributionDto>>(
    `${PERSONALITY_RESULTS_PATH}/distribution?${new URLSearchParams({ campusId }).toString()}`,
  );
}
