import { useMutation } from 'react-query';

import useSnackbar from '@material-hu/components/design-system/Snackbar';

import { downloadSessionReport } from 'src/pages/dashboard/Learning/Sessions/services';
import { type DownloadSessionReportParams } from 'src/pages/dashboard/Learning/Sessions/types';
import { useLokaliseTranslation } from 'src/utils/i18n';

export const useDownloadSessionReport = (sessionId?: number) => {
  const { enqueueSnackbar } = useSnackbar();
  const { t } = useLokaliseTranslation('learning');

  return useMutation({
    mutationFn: (params: Omit<DownloadSessionReportParams, 'sessionId'>) => {
      if (!sessionId) throw new Error('Session ID is required');
      return downloadSessionReport({ sessionId, ...params });
    },
    onSuccess: () => {
      enqueueSnackbar({
        title: t('reports.success.title'),
        variant: 'success',
      });
    },
    onError: () => {
      enqueueSnackbar({
        title: t('reports.error.sending_report'),
        variant: 'error',
      });
    },
  });
};
