import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { placesApi } from '@/db/api';
import { useTripSearch } from '../useTripSearch';

vi.mock('@/hooks/use-toast', () => ({
  useToast: () => ({
    toast: vi.fn(),
  }),
}));

vi.mock('@/db/api', () => ({
  placesApi: {
    search: vi.fn(),
  },
}));

describe('useTripSearch', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.useFakeTimers();
  });

  it('should initialize with empty states', () => {
    const { result } = renderHook(() => useTripSearch());
    expect(result.current.searchQuery).toBe('');
    expect(result.current.searchResults).toEqual([]);
    expect(result.current.isSearching).toBe(false);
  });

  it('should trigger search after debounce delay', async () => {
    const mockResults = [{ id: '1', name: 'Test Place' }];
    (placesApi.search as any).mockResolvedValue(mockResults);

    const { result } = renderHook(() => useTripSearch());
    
    act(() => {
      result.current.setSearchQuery('Paris');
    });

    expect(result.current.searchQuery).toBe('Paris');
    
    // Wait for debounce
    act(() => {
      vi.advanceTimersByTime(500);
    });

    // Check if search was called
    expect(placesApi.search).toHaveBeenCalledWith('Paris');
  });
});
