import { type UseQueryOptions, useQuery } from 'react-query';

import { type AxiosResponse } from 'axios';

import { getEvaluationQuestions } from 'src/services/courses';
import { type RequestError, type RequestSuccess } from 'src/types/services';

import { coursesKeys } from '../queries';
import { type Task } from '../types';

export type TSuccess = RequestSuccess<typeof getEvaluationQuestions>;
export type TError = RequestError;
export type TData = TSuccess['data'];
export type TParams = {
  task: Task | undefined;
  options?: UseQueryOptions<AxiosResponse<TData>, TError, TData>;
};

const useGetEvaluationsQuestions = ({ task, options }: TParams) => {
  const taskId = task?.id || '';
  const taskTag = task?.tag || '';

  return useQuery<TSuccess, TError, TData>(
    coursesKeys.lessons.questions(taskId, taskTag),
    () => getEvaluationQuestions(taskId, taskTag),
    {
      select: response => response.data,
      enabled: !!taskId && !!taskTag && options?.enabled,
      ...options,
    },
  );
};

export default useGetEvaluationsQuestions;
