import { apiClient } from '@/lib/api-client';
import type {
  ApprovalActionPayload,
  ApprovalDetailResponse,
  ApprovalFilters,
  ApprovalListResponse,
  ApprovalStepMutationPayload,
  ApprovalStepReorderPayload,
  ApprovalStepUpdatePayload,
  ApprovalWorkflowDetailResponse,
  ApprovalWorkflowFilters,
  ApprovalWorkflowListResponse,
  ApprovalWorkflowMutationPayload,
  ApprovalWorkflowStepListResponse,
  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(
  filters: ApprovalWorkflowFilters = {}
): Promise<ApprovalWorkflowListResponse> {
  const searchParams = new URLSearchParams();
  if (filters.entityType) searchParams.set('entityType', filters.entityType);
  if (filters.activeOnly) searchParams.set('activeOnly', 'true');

  const query = searchParams.toString();
  return apiClient<ApprovalWorkflowListResponse>(`${SETTINGS_BASE}${query ? `?${query}` : ''}`);
}

export async function getApprovalWorkflowById(id: string): Promise<ApprovalWorkflowDetailResponse> {
  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 cloneApprovalWorkflow(id: string) {
  return apiClient<MutationSuccessResponse>(`${SETTINGS_BASE}/${id}/clone`, {
    method: 'POST'
  });
}

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

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

export async function getApprovalWorkflowSteps(workflowId: string) {
  return apiClient<ApprovalWorkflowStepListResponse>(`${SETTINGS_BASE}/${workflowId}/steps`);
}

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

export async function updateApprovalWorkflowStep(
  workflowId: string,
  stepId: string,
  data: ApprovalStepUpdatePayload
) {
  return apiClient<MutationSuccessResponse>(`${SETTINGS_BASE}/${workflowId}/steps/${stepId}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteApprovalWorkflowStep(workflowId: string, stepId: string) {
  return apiClient<MutationSuccessResponse>(`${SETTINGS_BASE}/${workflowId}/steps/${stepId}`, {
    method: 'DELETE'
  });
}

export async function reorderApprovalWorkflowSteps(
  workflowId: string,
  data: ApprovalStepReorderPayload
) {
  return apiClient<MutationSuccessResponse>(`${SETTINGS_BASE}/${workflowId}/steps/reorder`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}
