import { apiClient } from '@/lib/api-client';
import type {
  CrmUserRoleAssignmentMutationPayload,
  CrmUserRoleAssignmentsResponse,
  CrmUserRoleAssignmentRecord,
  UpdateCrmUserRoleAssignmentPayload
} from './types';

export async function getCrmUserRoleAssignments() {
  return apiClient<CrmUserRoleAssignmentsResponse>('/crm/settings/user-role-assignments');
}

export async function createCrmUserRoleAssignment(
  payload: CrmUserRoleAssignmentMutationPayload
) {
  return apiClient<{ success: boolean; assignment: CrmUserRoleAssignmentRecord }>(
    '/crm/settings/user-role-assignments',
    {
      method: 'POST',
      body: JSON.stringify(payload)
    }
  );
}

export async function updateCrmUserRoleAssignment(
  id: string,
  payload: UpdateCrmUserRoleAssignmentPayload
) {
  return apiClient<{ success: boolean; assignment: CrmUserRoleAssignmentRecord }>(
    `/crm/settings/user-role-assignments/${id}`,
    {
      method: 'PATCH',
      body: JSON.stringify(payload)
    }
  );
}

export async function deleteCrmUserRoleAssignment(id: string) {
  return apiClient<{ success: boolean }>(`/crm/settings/user-role-assignments/${id}`, {
    method: 'DELETE'
  });
}
