'use client'

import { useMutation } from '@tanstack/react-query'
import { toast } from 'sonner'
import { forgotPasswordApi } from '@/lib/api/auth.api'
import type { ForgotPasswordRequest } from '@/types/auth'

export function useForgotPassword(options?: { onSuccess?: () => void }) {
  return useMutation({
    mutationFn: (payload: ForgotPasswordRequest) => forgotPasswordApi(payload),
    onSuccess: (data) => {
      toast.success(data.message)
      options?.onSuccess?.()
    },
    onError: (error: any) => {
      const message =
        error?.response?.data?.message ?? 'Failed to send reset code. Please try again.'
      toast.error(message)
    },
  })
}
