import { useModal } from '@material-hu/hooks/useModal';

import HuDialog from '@material-hu/components/design-system/Dialog';

import { useLokaliseTranslation } from 'src/utils/i18n';

import { type ColabListItem } from '../../types';

import useForceCompletion from './useForceCompletion';

type UseForceCompletionModalProps = {
  selectedColab: ColabListItem | null;
  courseName: string;
  courseId: number;
};

const useForceCompletionModal = ({
  selectedColab,
  courseName,
  courseId,
}: UseForceCompletionModalProps) => {
  const { t } = useLokaliseTranslation('learning');

  const forceCompletionMutation = useForceCompletion({
    courseId,
    courseName,
  });

  const { modal, showModal, closeModal } = useModal(
    HuDialog,
    { maxWidth: 'sm' },
    {
      title: t('reports.force_completion.title', {
        colaborator: selectedColab?.name,
      }),
      textBody: t('reports.force_completion.description'),
      primaryButtonProps: {
        children: t('reports.force_completion.action'),
        onClick: () => {
          if (selectedColab) {
            forceCompletionMutation.mutate(
              {
                userId: selectedColab.userId,
                previousStatus: selectedColab.courseUserStatus,
              },
              { onSuccess: () => closeModal() },
            );
          }
        },
      },
      secondaryButtonProps: {
        children: t('general:cancel'),
        onClick: () => closeModal(),
      },
    },
  );

  return {
    forceCompletionModal: modal,
    showForceCompletionModal: showModal,
    closeForceCompletionModal: closeModal,
  };
};

export default useForceCompletionModal;
