"use client"

import { useNotificationContext } from "@/component/Layout/NotificationContext"
import { useUploadFileIdProof, useUploadFilePermit } from "@/hook/useAuth"
import { useUploadImage } from "@/hook/useMedia"
import { useTranslations } from "next-intl"

export const handleUploadFileIdProof = () => {
  const transMessage = useTranslations("message")
  const transDocument = useTranslations("document")
  const { notify } = useNotificationContext()

  const { data: dataUploadFileIdProof, mutateAsync: callUploadFileIdProof } = useUploadFileIdProof()

  const uploadFileIdProof = async (options: any) => {
    const { file, onSuccess, onError } = options
    try {
      const response = await callUploadFileIdProof(file)
      if (response.data.success) {
        notify({
          type: "success",
          message: transMessage("success"),
          description: transDocument("add_file_success"),
        })
        onSuccess(response.data.data)
      } else {
        notify({
          type: "error",
          message: transMessage("fail"),
          description: response.data.msg || transMessage("fail"),
        })
        onError(new Error(response.data.msg))
      }
    } catch (error: any) {
      notify({
        type: "error",
        message: transMessage("fail"),
        description: error?.response?.data?.msgs || transMessage("error_during_upload"),
      })
      onError(error)
    }
  }

  return {
    dataUploadFileIdProof,
    uploadFileIdProof,
  }
}

export const handleUploadFilePermit = () => {
  const transMessage = useTranslations("message")
  const transDocument = useTranslations("document")
  const { notify } = useNotificationContext()

  const { data: dataUploadFilePermit, mutateAsync: callUploadFilePermit } = useUploadFilePermit()

  const uploadFilePermit = async (options: any) => {
    const { file, onSuccess, onError } = options
    try {
      const response = await callUploadFilePermit(file)
      if (response.data.success) {
        notify({
          type: "success",
          message: transMessage("success"),
          description: transDocument("add_file_success"),
        })
        onSuccess(response.data.data)
      } else {
        notify({
          type: "error",
          message: transMessage("fail"),
          description: response.data.msg || transMessage("fail"),
        })
        onError(new Error(response.data.msg))
      }
    } catch (error: any) {
      notify({
        type: "error",
        message: transMessage("fail"),
        description: error?.response?.data?.msgs || transMessage("error_during_upload"),
      })
      onError(error)
    }
  }

  return {
    dataUploadFilePermit,
    uploadFilePermit,
  }
}



export const handleUploadImage = () => {
  const transMessage = useTranslations("message")
  const transDocument = useTranslations("document")
  const { notify } = useNotificationContext()

  const { data: dataUploadImage, isPending, mutateAsync: callUploadImage } = useUploadImage()

  const uploadFileImage = async (options: any, onSuccessCallback?: any) => {
    const { file, onSuccess, onError } = options
    try {
      const response = await callUploadImage({ image: file })
      if (response.data.success) {
        notify({
          type: "success",
          message: transMessage("success"),
          description: transDocument("add_file_success"),
        })
        onSuccess(response.data.data)

        if (onSuccessCallback) {
          onSuccessCallback()
        }
      }
    } catch (error: any) {
      const message =
        error?.response?.data?.msgs && typeof error?.response?.data?.msgs === "object"
          ? Object.values(error?.response?.data?.msgs).join("<br />")
          : error?.response?.data?.msgs || transMessage("error_during_upload")
      notify({
        type: "error",
        message: transMessage("fail"),
        description: message,
      })
      onError(error)
    }
  }

  return {
    dataUploadImage,
    uploadFileImage,
    isPending
  }
}
