import { useMutation } from 'react-query';

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

import { downloadBulkCertificates } from 'src/pages/dashboard/Learning/Courses/services';
import { useLokaliseTranslation } from 'src/utils/i18n';
import { LogEvents, logEvent } from 'src/utils/logging';

type DownloadBulkCertificatesParams = {
  email: string;
  segmentationItemIds?: number[];
  segmentationUserIds?: number[];
};

export const useDownloadBulkCertificates = (courseId: number) => {
  const { enqueueSnackbar } = useSnackbar();
  const { t } = useLokaliseTranslation('learning');

  return useMutation(
    ({
      email,
      segmentationItemIds,
      segmentationUserIds,
    }: DownloadBulkCertificatesParams) =>
      downloadBulkCertificates(
        courseId,
        email,
        segmentationItemIds,
        segmentationUserIds,
      ),
    {
      onSuccess: () => {
        enqueueSnackbar({
          variant: 'success',
          title: t('reports.bulk_certificates.snackbar.success'),
        });
        logEvent(LogEvents.COURSE_DOWNLOAD_CERTIFICATE, { courseId });
      },
      onError: () => {
        enqueueSnackbar({
          variant: 'error',
          title: t('reports.bulk_certificates.snackbar.error'),
        });
      },
    },
  );
};
