import { apiClient } from '@/lib/api-client';
import type {
  ApprovalActionPayload,
  ApprovalDetailResponse,
  ApprovalFilters,
  ApprovalListResponse,
  ApprovalStepMutationPayload,
  ApprovalWorkflowDetailResponse,
  ApprovalWorkflowListResponse,
  ApprovalWorkflowMutationPayload,
  MutationSuccessResponse,
  SubmitApprovalPayload
} from './types';

export async function getApprovals(filters: ApprovalFilters): Promise<ApprovalListResponse> {
  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.entityType) searchParams.set('entityType', filters.entityType);
  if (filters.entityId) searchParams.set('entityId', filters.entityId);
  if (filters.sort) searchParams.set('sort', filters.sort);

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

export async function getApprovalById(id: string): Promise<ApprovalDetailResponse> {
  return apiClient<ApprovalDetailResponse>(`/crm/approvals/${id}`);
}

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

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

export async function approveApproval(id: string, data: ApprovalActionPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/approvals/${id}/approve`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function rejectApproval(id: string, data: ApprovalActionPayload) {
  return apiClient<MutationSuccessResponse>(`/crm/approvals/${id}/reject`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

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

export async function submitQuotationForApproval(id: string, remark?: string) {
  return apiClient<MutationSuccessResponse>(`/crm/quotations/${id}/submit-approval`, {
    method: 'POST',
    body: JSON.stringify({ remark })
  });
}

const SETTINGS_BASE = '/crm/settings/approval-workflows';

export async function getApprovalWorkflows() {
  return apiClient<ApprovalWorkflowListResponse>(SETTINGS_BASE);
}

export async function getApprovalWorkflowById(id: string) {
  return apiClient<ApprovalWorkflowDetailResponse>(`${SETTINGS_BASE}/${id}`);
}

export async function createApprovalWorkflow(data: ApprovalWorkflowMutationPayload) {
  return apiClient<MutationSuccessResponse>(SETTINGS_BASE, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

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

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

export async function replaceApprovalWorkflowSteps(
  workflowId: string,
  data: ApprovalStepMutationPayload[]
) {
  return apiClient<MutationSuccessResponse>(`${SETTINGS_BASE}/${workflowId}/steps`, {
    method: 'PUT',
    body: JSON.stringify({ steps: data })
  });
}
