import { apiClient } from '@/lib/api-client';
import type {
  DocumentTemplateDetailResponse,
  DocumentTemplateFilters,
  DocumentTemplateListResponse,
  DocumentTemplateMappingMutationPayload,
  DocumentTemplateMutationPayload,
  DocumentTemplateVersionActionResponse,
  DocumentTemplateVersionCompareResponse,
  DocumentTemplateVersionMutationPayload,
  DocumentTemplateVersionPreviewResponse,
  DocumentTemplateVersionValidateResponse,
  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(
  templateId: string,
  versionId: string,
  data: DocumentTemplateMappingMutationPayload,
) {
  void templateId;
  return apiClient<MutationSuccessResponse>(`${BASE_PATH}/versions/${versionId}/mappings`, {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

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

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

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

export async function validateDocumentTemplateVersion(id: string) {
  return apiClient<DocumentTemplateVersionValidateResponse>(`${BASE_PATH}/versions/${id}/validate`, {
    method: 'POST',
  });
}

export async function publishDocumentTemplateVersion(id: string) {
  return apiClient<DocumentTemplateVersionActionResponse>(`${BASE_PATH}/versions/${id}/publish`, {
    method: 'POST',
  });
}

export async function activateDocumentTemplateVersion(id: string) {
  return apiClient<DocumentTemplateVersionActionResponse>(`${BASE_PATH}/versions/${id}/activate`, {
    method: 'POST',
  });
}

export async function rollbackDocumentTemplateVersion(id: string) {
  return apiClient<DocumentTemplateVersionActionResponse>(`${BASE_PATH}/versions/${id}/rollback`, {
    method: 'POST',
  });
}

export async function archiveDocumentTemplateVersion(id: string) {
  return apiClient<DocumentTemplateVersionActionResponse>(`${BASE_PATH}/versions/${id}/archive`, {
    method: 'POST',
  });
}

export async function duplicateDocumentTemplateVersion(id: string) {
  return apiClient<DocumentTemplateVersionActionResponse>(`${BASE_PATH}/versions/${id}/duplicate`, {
    method: 'POST',
  });
}

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

export async function compareDocumentTemplateVersions(
  templateId: string,
  leftVersionId: string,
  rightVersionId: string,
) {
  return apiClient<DocumentTemplateVersionCompareResponse>(`${BASE_PATH}/${templateId}/compare`, {
    method: 'POST',
    body: JSON.stringify({ leftVersionId, rightVersionId }),
  });
}
