import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { useMutationWithToast } from '../use-mutation-with-toast'
import { addNotification } from '../../model/notification'

vi.mock('../../model/notification', () => ({
  addNotification: vi.fn(),
}))

function createWrapper() {
  const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
  return ({ children }: { children: React.ReactNode }) => (
    <QueryClientProvider client={qc}>{children}</QueryClientProvider>
  )
}

describe('useMutationWithToast', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  it('shows success notification on success with successMessage', async () => {
    const mutationFn = (): Promise<{ ok: boolean }> => Promise.resolve({ ok: true })
    const { result } = renderHook(() => useMutationWithToast({ mutationFn, successMessage: 'Done!' }), {
      wrapper: createWrapper(),
    })
    ;(result.current.mutate as () => void)()
    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(addNotification).toHaveBeenCalledWith({ type: 'success', message: 'Done!' })
  })

  it('does NOT show success notification without successMessage', async () => {
    const mutationFn = (): Promise<{ ok: boolean }> => Promise.resolve({ ok: true })
    const { result } = renderHook(() => useMutationWithToast({ mutationFn }), { wrapper: createWrapper() })
    ;(result.current.mutate as () => void)()
    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(addNotification).not.toHaveBeenCalled()
  })

  it('shows error notification on mutation error', async () => {
    const err = new Error('Boom')
    const mutationFn = (): Promise<never> => Promise.reject(err)
    const { result } = renderHook(() => useMutationWithToast({ mutationFn }), { wrapper: createWrapper() })
    ;(result.current.mutate as () => void)()
    await waitFor(() => expect(result.current.isError).toBe(true))
    expect(addNotification).toHaveBeenCalledWith({ type: 'error', message: 'Boom' })
  })

  it('calls user-provided onSuccess callback', async () => {
    const onSuccess: (data: { ok: boolean }, variables: void, onMutateResult: unknown, mutationContext: unknown) => void = vi.fn()
    const mutationFn = (): Promise<{ ok: boolean }> => Promise.resolve({ ok: true })
    const { result } = renderHook(() => useMutationWithToast({ mutationFn, onSuccess, successMessage: 'OK' }), {
      wrapper: createWrapper(),
    })
    ;(result.current.mutate as () => void)()
    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(onSuccess).toHaveBeenCalled()
  })

  it('calls user-provided onError callback', async () => {
    const onError: (error: Error, variables: void, onMutateResult: unknown, mutationContext: unknown) => void = vi.fn()
    const err = new Error('fail')
    const mutationFn = (): Promise<never> => Promise.reject(err)
    const { result } = renderHook(() => useMutationWithToast({ mutationFn, onError }), { wrapper: createWrapper() })
    ;(result.current.mutate as () => void)()
    await waitFor(() => expect(result.current.isError).toBe(true))
    expect(onError).toHaveBeenCalled()
  })
})
