import { apiClient } from '@/lib/api-client';
import type {
  ApprovalAutomationRunResponse,
  EscalationPoliciesResponse,
  EscalationPolicyPayload,
  MutationSuccessResponse,
  PendingApprovalQueueResponse,
  ReminderPoliciesResponse,
  ReminderPolicyPayload
} from './types';

const BASE = '/crm/approval';

export async function getPendingApprovalAutomationQueue() {
  return apiClient<PendingApprovalQueueResponse>(`${BASE}/automation/pending`);
}

export async function runApprovalAutomation() {
  return apiClient<ApprovalAutomationRunResponse>(`${BASE}/automation/run`, {
    method: 'POST'
  });
}

export async function getApprovalReminderPolicies() {
  return apiClient<ReminderPoliciesResponse>(`${BASE}/reminder-policies`);
}

export async function createApprovalReminderPolicy(data: ReminderPolicyPayload) {
  return apiClient<MutationSuccessResponse>(`${BASE}/reminder-policies`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateApprovalReminderPolicy(id: string, data: ReminderPolicyPayload) {
  return apiClient<MutationSuccessResponse>(`${BASE}/reminder-policies/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteApprovalReminderPolicy(id: string) {
  return apiClient<MutationSuccessResponse>(`${BASE}/reminder-policies/${id}`, {
    method: 'DELETE'
  });
}

export async function getApprovalEscalationPolicies() {
  return apiClient<EscalationPoliciesResponse>(`${BASE}/escalation-policies`);
}

export async function createApprovalEscalationPolicy(data: EscalationPolicyPayload) {
  return apiClient<MutationSuccessResponse>(`${BASE}/escalation-policies`, {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateApprovalEscalationPolicy(id: string, data: EscalationPolicyPayload) {
  return apiClient<MutationSuccessResponse>(`${BASE}/escalation-policies/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteApprovalEscalationPolicy(id: string) {
  return apiClient<MutationSuccessResponse>(`${BASE}/escalation-policies/${id}`, {
    method: 'DELETE'
  });
}
