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

// Mock haptic feedback
vi.mock('@/utils/haptics', () => ({
  triggerHapticFeedback: vi.fn(),
}));

import { triggerHapticFeedback } from '@/utils/haptics';

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

  it('should initialize with null activeId', () => {
    const mockReorder = vi.fn();
    const { result } = renderHook(() => useTripDragDrop(mockReorder));
    expect(result.current.activeId).toBeNull();
  });

  it('should initialize with sensors including Pointer, Touch, and Keyboard', () => {
    const mockReorder = vi.fn();
    const { result } = renderHook(() => useTripDragDrop(mockReorder));
    
    expect(result.current.sensors).toBeDefined();
    expect(result.current.sensors.length).toBeGreaterThanOrEqual(3);
  });

  it('should set activeId on handleDragStart', () => {
    const mockReorder = vi.fn();
    const { result } = renderHook(() => useTripDragDrop(mockReorder));
    
    act(() => {
      result.current.handleDragStart({ active: { id: 'item-1' } } as any);
    });

    expect(result.current.activeId).toBe('item-1');
    expect(triggerHapticFeedback).toHaveBeenCalledWith('selection');
  });

  it('should call reorderPlaces on handleDragEnd if item moved', async () => {
    const mockReorder = vi.fn();
    const { result } = renderHook(() => useTripDragDrop(mockReorder));
    
    await act(async () => {
      await result.current.handleDragEnd({
        active: { id: 'item-1' },
        over: { id: 'item-2' },
      } as any);
    });

    expect(mockReorder).toHaveBeenCalledWith('item-1', 'item-2');
    expect(result.current.activeId).toBeNull();
  });
});
