import { mutationOptions } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import {
  createCrmUserRoleAssignment,
  deleteCrmUserRoleAssignment,
  updateCrmUserRoleAssignment
} from './service';
import { crmUserRoleAssignmentQueryKeys } from './queries';

export const createCrmUserRoleAssignmentMutation = mutationOptions({
  mutationFn: createCrmUserRoleAssignment,
  onSettled: async () => {
    await getQueryClient().invalidateQueries({
      queryKey: crmUserRoleAssignmentQueryKeys.all
    });
  }
});

export const updateCrmUserRoleAssignmentMutation = mutationOptions({
  mutationFn: ({
    id,
    payload
  }: {
    id: string;
    payload: Parameters<typeof updateCrmUserRoleAssignment>[1];
  }) => updateCrmUserRoleAssignment(id, payload),
  onSettled: async () => {
    await getQueryClient().invalidateQueries({
      queryKey: crmUserRoleAssignmentQueryKeys.all
    });
  }
});

export const deleteCrmUserRoleAssignmentMutation = mutationOptions({
  mutationFn: deleteCrmUserRoleAssignment,
  onSettled: async () => {
    await getQueryClient().invalidateQueries({
      queryKey: crmUserRoleAssignmentQueryKeys.all
    });
  }
});
