import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  createDocument,
  deleteDocument,
  listPolicyDocuments,
  updateDocument,
} from '@/shared/api/documents';
import { apiRequest } from '@/shared/api/httpClient';
import type { DocumentMutationDto } from '@/shared/types/documents';

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

const apiRequestMock = vi.mocked(apiRequest);

const documentRequest: DocumentMutationDto = {
  entity_type: 'organization',
  entity_reference: 'Safety',
  name: 'Safety Policy',
  category: 'policy',
  uploaded_at: '2026-06-08T10:00:00.000Z',
  notes: 'Policy content',
};

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

  it('lists policy documents with the policy category query', () => {
    void listPolicyDocuments();

    expect(apiRequestMock).toHaveBeenCalledWith('/documents?category=policy');
  });

  it('creates documents with POST body wrapped in data', () => {
    void createDocument(documentRequest);

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

  it('updates documents with PUT body containing id and data', () => {
    void updateDocument('document-1', documentRequest);

    expect(apiRequestMock).toHaveBeenCalledWith('/documents/document-1', {
      method: 'PUT',
      body: {
        id: 'document-1',
        data: documentRequest,
      },
    });
  });

  it('deletes documents through the item endpoint', () => {
    void deleteDocument('document-1');

    expect(apiRequestMock).toHaveBeenCalledWith('/documents/document-1', {
      method: 'DELETE',
    });
  });
});
