import { apiRequest } from '@/shared/api/httpClient';
import type { ApiListResponse } from '@/shared/types/api';
import type {
  ContentCatalogDto,
  ContentCatalogMutationDto,
} from '@/shared/types/contentCatalog';

const PUBLIC_CONTENT_CATALOG_PATH = '/public/content-catalog';
const CONTENT_CATALOG_PATH = '/content-catalog';

export function getContentCatalog<TPayload>(contentType: string): Promise<ContentCatalogDto<TPayload>> {
  return apiRequest<ContentCatalogDto<TPayload>>(`${PUBLIC_CONTENT_CATALOG_PATH}/${encodeURIComponent(contentType)}`);
}

export function listManagedContentCatalog(): Promise<ApiListResponse<ContentCatalogDto<unknown>>> {
  return apiRequest<ApiListResponse<ContentCatalogDto<unknown>>>(CONTENT_CATALOG_PATH);
}

export function getManagedContentCatalog<TPayload>(contentType: string): Promise<ContentCatalogDto<TPayload>> {
  return apiRequest<ContentCatalogDto<TPayload>>(`${CONTENT_CATALOG_PATH}/${encodeURIComponent(contentType)}`);
}

export function createManagedContentCatalog<TPayload>(
  request: ContentCatalogMutationDto<TPayload> & { readonly content_type: string },
): Promise<ContentCatalogDto<TPayload>> {
  return apiRequest<ContentCatalogDto<TPayload>>(CONTENT_CATALOG_PATH, {
    method: 'POST',
    body: { data: request },
  });
}

export function updateManagedContentCatalog<TPayload>(
  contentType: string,
  request: ContentCatalogMutationDto<TPayload>,
): Promise<ContentCatalogDto<TPayload>> {
  return apiRequest<ContentCatalogDto<TPayload>>(`${CONTENT_CATALOG_PATH}/${encodeURIComponent(contentType)}`, {
    method: 'PUT',
    body: { data: request },
  });
}

export function deleteManagedContentCatalog(contentType: string): Promise<void> {
  return apiRequest<void>(`${CONTENT_CATALOG_PATH}/${encodeURIComponent(contentType)}`, {
    method: 'DELETE',
  });
}
