import useAxios from "axios-hooks";

const useApi = () => {
  const [{ ...addCommentData }, addComment] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleAddComment = async (
    question_activity_id,
    test_id,
    data,
    enterprise_id
  ) => {
    try {
      const url = enterprise_id
        ? `/learn/v1/instructor/${enterprise_id}/tests/${test_id}/add_comment/${question_activity_id}/`
        : `/learn/v1/instructor/tests/${test_id}/add_comment/${question_activity_id}/`;
      return await addComment({
        url,
        data,
      });
    } catch (err) {
      console.log(err);
    }
  };

  const [{ ...gradeQuestionData }, gradeQuestion] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleGradeQuestion = async (
    question_activity_id,
    session_id,
    data,
    enterprise_id
  ) => {
    try {
      const url = enterprise_id
        ? `/learn/v1/instructor/${enterprise_id}/tests/${question_activity_id}/submit_score/${session_id}/`
        : `/learn/v1/instructor/tests/${question_activity_id}/submit_score/${session_id}/`;
      return await gradeQuestion({
        url,
        data: {
          ...data,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  const [{ ...deleteCommentData }, deleteComment] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleDeleteComment = async (
    question_activity_id,
    test_id,
    index,
    enterprise_id
  ) => {
    try {
      const url = enterprise_id
        ? `/learn/v1/instructor/${enterprise_id}/tests/${test_id}/delete_comment/${question_activity_id}/?index=${index}`
        : `/learn/v1/instructor/tests/${test_id}/delete_comment/${question_activity_id}/?index=${index}`;
      return await deleteComment({
        url,
      });
    } catch (err) {
      console.log(err);
    }
  };
  const [{ ...uploadData }, upload] = useAxios(
    {
      method: "POST",
    },
    { manual: true }
  );

  const handleUpload = async (data) => {
    return await upload({
      url: "/media/v1/instructor/files/",
      data,
    });
  };

  return {
    addCommentData,
    handleAddComment,
    handleGradeQuestion,
    gradeQuestionData,
    deleteCommentData,
    handleDeleteComment,
    handleUpload,
    uploadData,
  };
};

export default useApi;
