import { apiClient } from '@/lib/api-client';
import type {
  MutationSuccessResponse,
  OpportunityAssignmentMutationPayload,
  OpportunityAttachmentsResponse,
  OpportunityDetailResponse,
  OpportunityFilters,
  OpportunityFollowupMutationPayload,
  OpportunityFollowupsResponse,
  OpportunityListResponse,
  OpportunityMarkCancelledPayload,
  OpportunityMarkLostPayload,
  OpportunityMarkNoQuotationPayload,
  OpportunityMarkWonPayload,
  OpportunityMutationPayload,
  OpportunityProjectPartiesResponse,
  OpportunityReopenPayload
} from './types';

export async function getOpportunities(filters: OpportunityFilters): Promise<OpportunityListResponse> {
  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.stage) searchParams.set('stage', filters.stage);
  if (filters.outcomeStatus) searchParams.set('outcomeStatus', filters.outcomeStatus);
  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.lostReason) searchParams.set('lostReason', filters.lostReason);
  if (filters.closedDateFrom) searchParams.set('closedDateFrom', filters.closedDateFrom);
  if (filters.closedDateTo) searchParams.set('closedDateTo', filters.closedDateTo);
  if (filters.sort) searchParams.set('sort', filters.sort);

  const query = searchParams.toString();
  return apiClient<OpportunityListResponse>(`/crm/opportunities${query ? `?${query}` : ''}`);
}

export async function getOpportunityById(id: string): Promise<OpportunityDetailResponse> {
  return apiClient<OpportunityDetailResponse>(`/crm/opportunities/${id}`);
}

export async function getOpportunityProjectParties(id: string): Promise<OpportunityProjectPartiesResponse> {
  return apiClient<OpportunityProjectPartiesResponse>(`/crm/opportunities/${id}/customers`);
}

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

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

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

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

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

export async function getOpportunityFollowups(id: string): Promise<OpportunityFollowupsResponse> {
  return apiClient<OpportunityFollowupsResponse>(`/crm/opportunities/${id}/followups`);
}

export async function getOpportunityAttachments(id: string): Promise<OpportunityAttachmentsResponse> {
  return apiClient<OpportunityAttachmentsResponse>(`/crm/opportunities/${id}/po-attachments`);
}

export async function createOpportunityFollowup(
  opportunityId: string,
  data: OpportunityFollowupMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${opportunityId}/followups`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateOpportunityFollowup(
  opportunityId: string,
  followupId: string,
  data: OpportunityFollowupMutationPayload
) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${opportunityId}/followups/${followupId}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

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

export async function markOpportunityWon(id: string, data: OpportunityMarkWonPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${id}/mark-won`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function markOpportunityLost(id: string, data: OpportunityMarkLostPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${id}/mark-lost`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function markOpportunityCancelled(id: string, data: OpportunityMarkCancelledPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${id}/mark-cancelled`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function markOpportunityNoQuotation(id: string, data: OpportunityMarkNoQuotationPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${id}/mark-no-quotation`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function reopenOpportunity(id: string, data: OpportunityReopenPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/opportunities/${id}/reopen`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}
