import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  getPersonalityCompletion,
  getCurrentPersonalityResult,
  listCurrentPersonalityResultHistory,
  listPersonalityDistribution,
  saveCurrentPersonalityResult,
} from '@/shared/api/personality';
import { apiRequest } from '@/shared/api/httpClient';
import type { PersonalityQuizResultMutationDto } from '@/shared/types/personality';
import { PERSONALITY_QUIZ_KINDS } from '@/shared/constants/personality';

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

const apiRequestMock = vi.mocked(apiRequest);

describe('personality API', () => {
  beforeEach(() => {
    apiRequestMock.mockReset();
  });

  it('loads the current user personality result through the me endpoint', () => {
    void getCurrentPersonalityResult(PERSONALITY_QUIZ_KINDS.personalityType);

    expect(apiRequestMock).toHaveBeenCalledWith('/personality_quiz_results/me?quiz_kind=personality_type');
  });

  it('loads the current user personality result history through the me history endpoint', () => {
    void listCurrentPersonalityResultHistory();

    expect(apiRequestMock).toHaveBeenCalledWith('/personality_quiz_results/me/history');
  });

  it('saves the current user personality result with PUT body wrapped in data', () => {
    const request: PersonalityQuizResultMutationDto = {
      quiz_kind: PERSONALITY_QUIZ_KINDS.personalityType,
      quiz_id: 'personality-type',
      quiz_title: 'Personality Type Quiz',
      personality_type: 'INFJ',
      quiz_answers: {
        '1': 'I',
        '2': 'N',
      },
      total_questions: 2,
    };

    void saveCurrentPersonalityResult(request);

    expect(apiRequestMock).toHaveBeenCalledWith('/personality_quiz_results/me', {
      method: 'PUT',
      body: { data: request },
    });
  });

  it('lists personality distribution without campus filtering by default', () => {
    void listPersonalityDistribution();

    expect(apiRequestMock).toHaveBeenCalledWith('/personality_quiz_results/distribution');
  });

  it('lists personality distribution with campus filtering', () => {
    void listPersonalityDistribution('campus-1');

    expect(apiRequestMock).toHaveBeenCalledWith('/personality_quiz_results/distribution?campusId=campus-1');
  });

  it('loads the personality completion report', () => {
    void getPersonalityCompletion();

    expect(apiRequestMock).toHaveBeenCalledWith('/personality_quiz_results/completion');
  });
});
