import { apiClient } from '@/lib/api-client';
import type {
  DocumentTemplateDetailResponse,
  DocumentTemplateFilters,
  DocumentTemplateListResponse,
  DocumentTemplateMappingMutationPayload,
  DocumentTemplateMutationPayload,
  DocumentTemplateVersionMutationPayload,
  DocumentTemplateVersionsResponse,
  MutationSuccessResponse
} from './types';

const BASE_PATH = '/crm/settings/document-templates';

export async function getDocumentTemplates(filters: DocumentTemplateFilters = {}) {
  const searchParams = new URLSearchParams();

  if (filters.documentType) searchParams.set('documentType', filters.documentType);
  if (filters.fileType) searchParams.set('fileType', filters.fileType);
  if (filters.isActive) searchParams.set('isActive', filters.isActive);

  const query = searchParams.toString();
  return apiClient<DocumentTemplateListResponse>(`${BASE_PATH}${query ? `?${query}` : ''}`);
}

export async function getDocumentTemplateById(id: string) {
  return apiClient<DocumentTemplateDetailResponse>(`${BASE_PATH}/${id}`);
}

export async function createDocumentTemplate(data: DocumentTemplateMutationPayload) {
  return apiClient<MutationSuccessResponse>(BASE_PATH, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateDocumentTemplate(
  id: string,
  data: Partial<DocumentTemplateMutationPayload>
) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteDocumentTemplate(id: string) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/${id}`, {
    method: 'DELETE'
  });
}

export async function getDocumentTemplateVersions(id: string) {
  return apiClient<DocumentTemplateVersionsResponse>(`${BASE_PATH}/${id}/versions`);
}

export async function createDocumentTemplateVersion(
  id: string,
  data: DocumentTemplateVersionMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/${id}/versions`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateDocumentTemplateVersion(
  id: string,
  data: Partial<DocumentTemplateVersionMutationPayload>
) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/versions/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function setDocumentTemplateVersionActive(id: string, isActive: boolean) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/versions/${id}`, {
    method: 'PATCH',
    body: JSON.stringify({ isActive })
  });
}

export async function createDocumentTemplateMapping(
  versionId: string,
  data: DocumentTemplateMappingMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/versions/${versionId}/mappings`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateDocumentTemplateMapping(
  id: string,
  data: Partial<DocumentTemplateMappingMutationPayload>
) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/mappings/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteDocumentTemplateMapping(id: string) {
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/mappings/${id}`, {
    method: 'DELETE'
  });
}
