// hooks/useUpdateImage.ts (updated)
import { IBaseResponse } from '@/@core/types/api'
import { useCustomMutation } from '@/hooks/api'
import { request } from '@/utils'

export const UPLOAD_IMAGE_QUERY_KEY = 'update-image'

export const useUpdateImage = () => {
  const uploadImage = useCustomMutation<
    FormData | { image: File; path: string },
    IBaseResponse<{ image_path: string }>
  >([UPLOAD_IMAGE_QUERY_KEY], data => {
    // Accept both shapes; always POST multipart/form-data
    const body =
      data instanceof FormData
        ? data
        : (() => {
            const fd = new FormData()
            fd.append('image', data.image)
            fd.append('path', data.path)
            return fd
          })()

    return request<IBaseResponse<{ image_path: string }>, FormData>({
      url: `dashboard/upload-image`,
      method: 'POST',
      data: body,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  })

  return uploadImage
}
