import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  createManagedContentCatalog,
  deleteManagedContentCatalog,
  getContentCatalog,
  getManagedContentCatalog,
  listManagedContentCatalog,
  updateManagedContentCatalog,
} from '@/shared/api/contentCatalog';
import { apiRequest } from '@/shared/api/httpClient';

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

const apiRequestMock = vi.mocked(apiRequest);

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

  it('loads a content catalog payload by type', () => {
    void getContentCatalog('parent-message-templates');

    expect(apiRequestMock).toHaveBeenCalledWith('/public/content-catalog/parent-message-templates');
  });

  it('loads managed content catalog records', () => {
    void listManagedContentCatalog();
    void getManagedContentCatalog('safety-qbs-quiz');

    expect(apiRequestMock).toHaveBeenNthCalledWith(1, '/content-catalog');
    expect(apiRequestMock).toHaveBeenNthCalledWith(2, '/content-catalog/safety-qbs-quiz');
  });

  it('creates, updates, and deletes managed content catalog records', () => {
    const payload = { title: 'Quiz' };

    void createManagedContentCatalog({
      content_type: 'safety-qbs-quiz',
      payload,
    });
    void updateManagedContentCatalog('safety-qbs-quiz', { payload });
    void deleteManagedContentCatalog('safety-qbs-quiz');

    expect(apiRequestMock).toHaveBeenNthCalledWith(1, '/content-catalog', {
      method: 'POST',
      body: {
        data: {
          content_type: 'safety-qbs-quiz',
          payload,
        },
      },
    });
    expect(apiRequestMock).toHaveBeenNthCalledWith(2, '/content-catalog/safety-qbs-quiz', {
      method: 'PUT',
      body: {
        data: { payload },
      },
    });
    expect(apiRequestMock).toHaveBeenNthCalledWith(3, '/content-catalog/safety-qbs-quiz', {
      method: 'DELETE',
    });
  });
});
