import { apiRequest } from '@/shared/api/httpClient';
import { ApiListResponse } from '@/shared/types/api';
import type { DocumentDto, DocumentMutationDto } from '@/shared/types/documents';

const DOCUMENTS_PATH = '/documents';

function createDocumentsPath(params: URLSearchParams): string {
  const query = params.toString();
  return query ? `${DOCUMENTS_PATH}?${query}` : DOCUMENTS_PATH;
}

export function listPolicyDocuments(): Promise<ApiListResponse<DocumentDto>> {
  return apiRequest<ApiListResponse<DocumentDto>>(
    createDocumentsPath(new URLSearchParams({ category: 'policy' })),
  );
}

export function createDocument(
  request: DocumentMutationDto,
): Promise<DocumentDto> {
  return apiRequest<DocumentDto>(DOCUMENTS_PATH, {
    method: 'POST',
    body: { data: request },
  });
}

export function updateDocument(
  id: string,
  request: DocumentMutationDto,
): Promise<DocumentDto> {
  return apiRequest<DocumentDto>(`${DOCUMENTS_PATH}/${id}`, {
    method: 'PUT',
    body: {
      id,
      data: request,
    },
  });
}

export function deleteDocument(id: string): Promise<boolean> {
  return apiRequest<boolean>(`${DOCUMENTS_PATH}/${id}`, {
    method: 'DELETE',
  });
}
