// src/hooks/mutations/useCreateShoppingListMutation.test.tsx
import { renderHook, waitFor } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { ReactNode } from 'react';
import { useCreateShoppingListMutation } from './useCreateShoppingListMutation';
import * as apiClient from '../../services/apiClient';
import * as notificationService from '../../services/notificationService';

vi.mock('../../services/apiClient');
vi.mock('../../services/notificationService');

const mockedApiClient = vi.mocked(apiClient);
const mockedNotifications = vi.mocked(notificationService);

describe('useCreateShoppingListMutation', () => {
  let queryClient: QueryClient;

  const wrapper = ({ children }: { children: ReactNode }) => (
    <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  );

  beforeEach(() => {
    vi.resetAllMocks();
    queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
  });

  it('should create a shopping list successfully', async () => {
    const mockResponse = { shopping_list_id: 1, name: 'Weekly Groceries' };
    mockedApiClient.createShoppingList.mockResolvedValue({
      ok: true,
      json: () => Promise.resolve(mockResponse),
    } as Response);

    const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });

    result.current.mutate({ name: 'Weekly Groceries' });

    await waitFor(() => expect(result.current.isSuccess).toBe(true));

    expect(mockedApiClient.createShoppingList).toHaveBeenCalledWith('Weekly Groceries');
    expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Shopping list created');
  });

  it('should invalidate shopping-lists query on success', async () => {
    mockedApiClient.createShoppingList.mockResolvedValue({
      ok: true,
      json: () => Promise.resolve({ shopping_list_id: 1 }),
    } as Response);

    const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');

    const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });

    result.current.mutate({ name: 'Test List' });

    await waitFor(() => expect(result.current.isSuccess).toBe(true));

    expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['shopping-lists'] });
  });

  it('should handle API error with error message', async () => {
    mockedApiClient.createShoppingList.mockResolvedValue({
      ok: false,
      status: 400,
      json: () => Promise.resolve({ message: 'List name already exists' }),
    } as Response);

    const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });

    result.current.mutate({ name: 'Duplicate List' });

    await waitFor(() => expect(result.current.isError).toBe(true));

    expect(result.current.error?.message).toBe('List name already exists');
    expect(mockedNotifications.notifyError).toHaveBeenCalledWith('List name already exists');
  });

  it('should handle API error when json parse fails', async () => {
    mockedApiClient.createShoppingList.mockResolvedValue({
      ok: false,
      status: 500,
      json: () => Promise.reject(new Error('Parse error')),
    } as Response);

    const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });

    result.current.mutate({ name: 'Test' });

    await waitFor(() => expect(result.current.isError).toBe(true));

    expect(result.current.error?.message).toBe('Request failed with status 500');
  });

  it('should handle API error with empty message in response', async () => {
    mockedApiClient.createShoppingList.mockResolvedValue({
      ok: false,
      status: 400,
      json: () => Promise.resolve({ message: '' }),
    } as Response);

    const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });

    result.current.mutate({ name: 'Empty Error' });

    await waitFor(() => expect(result.current.isError).toBe(true));

    expect(result.current.error?.message).toBe('Failed to create shopping list');
  });

  it('should use fallback error message when error has no message', async () => {
    mockedApiClient.createShoppingList.mockRejectedValue(new Error(''));

    const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });

    result.current.mutate({ name: 'New List' });

    await waitFor(() => expect(result.current.isError).toBe(true));

    expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Failed to create shopping list');
  });
});
