import { GetConversationsHandler } from '../queries/get-conversations/get-conversations.handler';
import { GetConversationsQuery } from '../queries/get-conversations/get-conversations.query';

describe('GetConversationsHandler', () => {
  let handler: GetConversationsHandler;
  let mockConversationRepo: {
    findByUserId: ReturnType<typeof vi.fn>;
    countByUserId: ReturnType<typeof vi.fn>;
  };

  const conversations = [
    {
      id: 'conv-1',
      listingId: 'listing-1',
      subject: null,
      status: 'ACTIVE',
      lastMessage: 'Hello!',
      lastMessageAt: new Date(),
      createdAt: new Date(),
      updatedAt: new Date(),
      participants: [],
    },
  ];

  beforeEach(() => {
    mockConversationRepo = {
      findByUserId: vi.fn().mockResolvedValue(conversations),
      countByUserId: vi.fn().mockResolvedValue(1),
    };

    handler = new GetConversationsHandler(mockConversationRepo as any);
  });

  it('returns conversations with total count', async () => {
    const query = new GetConversationsQuery('user-1', 20, 0);

    const result = await handler.execute(query);

    expect(result.conversations).toHaveLength(1);
    expect(result.total).toBe(1);
    expect(mockConversationRepo.findByUserId).toHaveBeenCalledWith('user-1', 20, 0);
    expect(mockConversationRepo.countByUserId).toHaveBeenCalledWith('user-1');
  });
});
