import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  deleteUserProgressByItem,
  listUserProgress,
  upsertUserProgress,
} from '@/shared/api/userProgress';
import { apiRequest } from '@/shared/api/httpClient';
import type { UserProgressMutationDto } from '@/shared/types/userProgress';

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

const apiRequestMock = vi.mocked(apiRequest);

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

  it('lists progress by required progress type', () => {
    void listUserProgress('sign_learned');

    expect(apiRequestMock).toHaveBeenCalledWith('/user_progress?progress_type=sign_learned');
  });

  it('lists progress by type and item id', () => {
    void listUserProgress('zone_checkin', 'green');

    expect(apiRequestMock).toHaveBeenCalledWith('/user_progress?progress_type=zone_checkin&item_id=green');
  });

  it('upserts progress with POST body wrapped in data', () => {
    const request: UserProgressMutationDto = {
      progress_type: 'zone_checkin',
      item_id: 'green',
      value: 'regulated',
      score: 4,
      metadata: { note: 'Ready to learn' },
    };

    void upsertUserProgress(request);

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

  it('deletes progress by item through the typed DELETE endpoint', () => {
    void deleteUserProgressByItem('sign_learned', 'help');

    expect(apiRequestMock).toHaveBeenCalledWith('/user_progress/by-item?progress_type=sign_learned&item_id=help', {
      method: 'DELETE',
    });
  });
});
