import type { IInquiryRepository } from '../../domain/repositories/inquiry.repository';
import { GetInquiriesByListingHandler } from '../queries/get-inquiries-by-listing/get-inquiries-by-listing.handler';
import { GetInquiriesByListingQuery } from '../queries/get-inquiries-by-listing/get-inquiries-by-listing.query';

describe('GetInquiriesByListingHandler', () => {
  let handler: GetInquiriesByListingHandler;
  let mockInquiryRepo: { [K in keyof IInquiryRepository]: ReturnType<typeof vi.fn> };

  beforeEach(() => {
    mockInquiryRepo = {
      findById: vi.fn(),
      save: vi.fn(),
      markAsRead: vi.fn(),
      findByListing: vi.fn(),
      findByAgent: vi.fn(),
      countUnreadByAgent: vi.fn(),
    };

    handler = new GetInquiriesByListingHandler(mockInquiryRepo as any);
  });

  it('returns paginated results', async () => {
    const mockResult = {
      data: [
        {
          id: 'inq-1',
          listingId: 'listing-1',
          userId: 'user-1',
          message: 'Tôi muốn xem nhà',
          phone: '0901234567',
          isRead: false,
          createdAt: new Date(),
        },
      ],
      total: 1,
      page: 1,
      limit: 20,
      totalPages: 1,
    };
    mockInquiryRepo.findByListing.mockResolvedValue(mockResult);

    const query = new GetInquiriesByListingQuery('listing-1', 1, 20);
    const result = await handler.execute(query);

    expect(result).toEqual(mockResult);
    expect(result.data).toHaveLength(1);
    expect(result.total).toBe(1);
    expect(result.page).toBe(1);
    expect(mockInquiryRepo.findByListing).toHaveBeenCalledWith('listing-1', 1, 20);
  });

  it('returns empty data when no inquiries found', async () => {
    const mockResult = {
      data: [],
      total: 0,
      page: 1,
      limit: 20,
      totalPages: 0,
    };
    mockInquiryRepo.findByListing.mockResolvedValue(mockResult);

    const query = new GetInquiriesByListingQuery('listing-empty', 1, 20);
    const result = await handler.execute(query);

    expect(result.data).toHaveLength(0);
    expect(result.total).toBe(0);
  });
});
