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

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

describe('useTripEvents', () => {
  const mockTrip = { id: 'trip-1' } as any;
  const mockLoadTrip = vi.fn();
  const mockAddPlace = vi.fn();
  const mockRemovePlace = vi.fn();

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('should call addPlace when handleAddPlace is called with activeDayId', async () => {
    const { result } = renderHook(() => 
      useTripEvents(mockTrip, 'day-1', mockLoadTrip, mockAddPlace, mockRemovePlace)
    );
    
    await act(async () => {
      await result.current.handleAddPlace('place-123');
    });

    expect(mockAddPlace).toHaveBeenCalledWith('place-123', 'day-1');
  });

  it('should call removePlace when handleDeletePlace is confirmed', async () => {
    vi.stubGlobal('confirm', vi.fn().mockReturnValue(true));
    
    const { result } = renderHook(() => 
      useTripEvents(mockTrip, 'day-1', mockLoadTrip, mockAddPlace, mockRemovePlace)
    );
    
    await act(async () => {
      await result.current.handleDeletePlace('tp-1', 'Test Place');
    });

    expect(mockRemovePlace).toHaveBeenCalledWith('tp-1');
  });

  it('should copy to clipboard when handleShare is called', async () => {
    const mockClipboard = {
      writeText: vi.fn().mockResolvedValue(undefined),
    };
    vi.stubGlobal('navigator', { clipboard: mockClipboard });

    const { result } = renderHook(() => 
      useTripEvents(mockTrip, 'day-1', mockLoadTrip, mockAddPlace, mockRemovePlace)
    );
    
    await act(async () => {
      await result.current.handleShare();
    });

    expect(mockClipboard.writeText).toHaveBeenCalled();
  });
});
