import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  getCurrentPersonalityResult,
  listPersonalityDistribution,
  saveCurrentPersonalityResult,
} from '@/shared/api/personality';
import { apiRequest } from '@/shared/api/httpClient';
import type { PersonalityQuizResultMutationDto } from '@/shared/types/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();

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

  it('saves the current user personality result with PUT body wrapped in data', () => {
    const request: PersonalityQuizResultMutationDto = {
      personality_type: 'INFJ',
      quiz_answers: {
        '1': 'I',
        '2': 'N',
      },
    };

    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');
  });
});
