import { useMutation, useQueryClient } from '@tanstack/react-query';
import { partnerCenterApi } from '../apis';
import { centerKeys } from '../keys';

/** Set/clear a nurse's sponsorship link to a center. Invalidate the roster + detail. */
export function useAssignNurseToPartnerCenter(id: number) {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { nurseProfileId: number; unlink: boolean }>({
    mutationFn: ({ nurseProfileId, unlink }) => partnerCenterApi.assignNurse(id, nurseProfileId, unlink),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: centerKeys.sponsoredNurses(id) });
      queryClient.invalidateQueries({ queryKey: centerKeys.detail(id) });
      queryClient.invalidateQueries({ queryKey: centerKeys.lists() });
    },
  });
}
