import { useMutation } from 'react-query';

import { type SegmentationPayload } from '../../common/types';
import { assignUsersToSession } from '../services';

import { useNewSessionStepper } from './useNewSessionStepper';

export const useAssignUsersToSession = (sessionId?: number) => {
  const { next } = useNewSessionStepper();

  return useMutation({
    mutationFn: (data: SegmentationPayload) => {
      if (!sessionId) throw new Error('Session ID is required');
      return assignUsersToSession(sessionId, data);
    },
    onSuccess: () => next.handle(),
  });
};
