import { apiClient } from '@/lib/api-client';
import type {
  EnquiryAssignmentMutationPayload,
  EnquiryDetailResponse,
  EnquiryFilters,
  EnquiryFollowupMutationPayload,
  EnquiryFollowupsResponse,
  EnquiryListResponse,
  EnquiryMutationPayload,
  EnquiryProjectPartiesResponse,
  MutationSuccessResponse
} from './types';

export async function getEnquiries(filters: EnquiryFilters): Promise<EnquiryListResponse> {
  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.pipelineStage) searchParams.set('pipelineStage', filters.pipelineStage);
  if (filters.status) searchParams.set('status', filters.status);
  if (filters.productType) searchParams.set('productType', filters.productType);
  if (filters.priority) searchParams.set('priority', filters.priority);
  if (filters.branch) searchParams.set('branch', filters.branch);
  if (filters.customer) searchParams.set('customer', filters.customer);
  if (filters.sort) searchParams.set('sort', filters.sort);

  const query = searchParams.toString();

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

export async function getEnquiryById(id: string): Promise<EnquiryDetailResponse> {
  return apiClient<EnquiryDetailResponse>(`/crm/enquiries/${id}`);
}

export async function getEnquiryProjectParties(id: string): Promise<EnquiryProjectPartiesResponse> {
  return apiClient<EnquiryProjectPartiesResponse>(`/crm/enquiries/${id}/customers`);
}

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

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

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

export async function assignEnquiry(id: string, data: EnquiryAssignmentMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/enquiries/${id}/assign`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function reassignEnquiry(id: string, data: EnquiryAssignmentMutationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/enquiries/${id}/reassign`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function getEnquiryFollowups(id: string): Promise<EnquiryFollowupsResponse> {
  return apiClient<EnquiryFollowupsResponse>(`/crm/enquiries/${id}/followups`);
}

export async function createEnquiryFollowup(
  enquiryId: string,
  data: EnquiryFollowupMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/enquiries/${enquiryId}/followups`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateEnquiryFollowup(
  enquiryId: string,
  followupId: string,
  data: EnquiryFollowupMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/enquiries/${enquiryId}/followups/${followupId}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteEnquiryFollowup(enquiryId: string, followupId: string) {
  return apiClient<MutationSuccessResponse>(`/crm/enquiries/${enquiryId}/followups/${followupId}`, {
    method: 'DELETE'
  });
}
