import { apiClient } from '@/lib/api-client';
import type {
  MutationSuccessResponse,
  QuotationAttachmentMutationPayload,
  QuotationAttachmentsResponse,
  QuotationCustomerMutationPayload,
  QuotationCustomersResponse,
  QuotationDetailResponse,
  QuotationFilters,
  QuotationFollowupMutationPayload,
  QuotationFollowupsResponse,
  QuotationItemMutationPayload,
  QuotationItemsResponse,
  QuotationListResponse,
  QuotationMutationPayload,
  QuotationRevisionsResponse,
  QuotationTopicMutationPayload,
  QuotationTopicsResponse
} from './types';

export async function getQuotations(filters: QuotationFilters): Promise<QuotationListResponse> {
  const searchParams = new URLSearchParams();

  if (filters.page) searchParams.set('page', String(filters.page));
  if (filters.limit) searchParams.set('limit', String(filters.limit));
  if (filters.search) searchParams.set('search', filters.search);
  if (filters.status) searchParams.set('status', filters.status);
  if (filters.quotationType) searchParams.set('quotationType', filters.quotationType);
  if (filters.branch) searchParams.set('branch', filters.branch);
  if (filters.customer) searchParams.set('customer', filters.customer);
  if (filters.enquiry) searchParams.set('enquiry', filters.enquiry);
  if (filters.hot) searchParams.set('hot', filters.hot);
  if (filters.sort) searchParams.set('sort', filters.sort);

  const query = searchParams.toString();

  return apiClient<QuotationListResponse>(`/crm/quotations${query ? `?${query}` : ''}`);
}

export async function getQuotationById(id: string): Promise<QuotationDetailResponse> {
  return apiClient<QuotationDetailResponse>(`/crm/quotations/${id}`);
}

export async function createQuotation(data: QuotationMutationPayload) {
  return apiClient<MutationSuccessResponse>('/crm/quotations', {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateQuotation(id: string, data: QuotationMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

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

export async function getQuotationItems(id: string): Promise<QuotationItemsResponse> {
  return apiClient<QuotationItemsResponse>(`/crm/quotations/${id}/items`);
}

export async function createQuotationItem(id: string, data: QuotationItemMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/items`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateQuotationItem(
  quotationId: string,
  itemId: string,
  data: QuotationItemMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/items/${itemId}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteQuotationItem(quotationId: string, itemId: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/items/${itemId}`, {
    method: 'DELETE'
  });
}

export async function getQuotationCustomers(id: string): Promise<QuotationCustomersResponse> {
  return apiClient<QuotationCustomersResponse>(`/crm/quotations/${id}/customers`);
}

export async function createQuotationCustomer(id: string, data: QuotationCustomerMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/customers`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateQuotationCustomer(
  quotationId: string,
  relationId: string,
  data: QuotationCustomerMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/customers`, {
    method: 'PATCH',
    body: JSON.stringify({ id: relationId, ...data })
  });
}

export async function deleteQuotationCustomer(quotationId: string, relationId: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/customers`, {
    method: 'DELETE',
    body: JSON.stringify({ id: relationId })
  });
}

export async function getQuotationTopics(id: string): Promise<QuotationTopicsResponse> {
  return apiClient<QuotationTopicsResponse>(`/crm/quotations/${id}/topics`);
}

export async function createQuotationTopic(id: string, data: QuotationTopicMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/topics`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateQuotationTopic(id: string, data: QuotationTopicMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/topics`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteQuotationTopic(quotationId: string, topicId: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/topics`, {
    method: 'DELETE',
    body: JSON.stringify({ id: topicId })
  });
}

export async function getQuotationFollowups(id: string): Promise<QuotationFollowupsResponse> {
  return apiClient<QuotationFollowupsResponse>(`/crm/quotations/${id}/followups`);
}

export async function createQuotationFollowup(id: string, data: QuotationFollowupMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/followups`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateQuotationFollowup(
  quotationId: string,
  data: QuotationFollowupMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/followups`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteQuotationFollowup(quotationId: string, followupId: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/followups`, {
    method: 'DELETE',
    body: JSON.stringify({ id: followupId })
  });
}

export async function getQuotationAttachments(id: string): Promise<QuotationAttachmentsResponse> {
  return apiClient<QuotationAttachmentsResponse>(`/crm/quotations/${id}/attachments`);
}

export async function createQuotationAttachment(
  id: string,
  data: QuotationAttachmentMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/attachments`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateQuotationAttachment(
  quotationId: string,
  data: QuotationAttachmentMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/attachments`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteQuotationAttachment(quotationId: string, attachmentId: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${quotationId}/attachments`, {
    method: 'DELETE',
    body: JSON.stringify({ id: attachmentId })
  });
}

export async function getQuotationRevisions(id: string): Promise<QuotationRevisionsResponse> {
  return apiClient<QuotationRevisionsResponse>(`/crm/quotations/${id}/revisions`);
}

export async function createQuotationRevision(id: string, revisionRemark?: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/revisions`, {
    method: 'POST',
    body: JSON.stringify({ revisionRemark })
  });
}
