import { type IListingRepository } from '@modules/listings/domain/repositories/listing.repository';
import { GetPendingModerationHandler } from '../queries/get-pending-moderation/get-pending-moderation.handler';
import { GetPendingModerationQuery } from '../queries/get-pending-moderation/get-pending-moderation.query';

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

  beforeEach(() => {
    mockListingRepo = {
      findById: vi.fn(),
      findByIdWithProperty: vi.fn(),
      save: vi.fn(),
      update: vi.fn(),
      search: vi.fn(),
      findByStatus: vi.fn(),
      findBySellerId: vi.fn(),
    };

    handler = new GetPendingModerationHandler(mockListingRepo as any);
  });

  it('returns paginated pending listings', async () => {
    const mockResult = {
      data: [{ id: 'listing-1', status: 'PENDING_REVIEW' }],
      total: 1,
      page: 1,
      limit: 20,
      totalPages: 1,
    };
    mockListingRepo.findByStatus.mockResolvedValue(mockResult);

    const query = new GetPendingModerationQuery(1, 20);
    const result = await handler.execute(query);

    expect(result).toEqual(mockResult);
    expect(mockListingRepo.findByStatus).toHaveBeenCalledWith('PENDING_REVIEW', 1, 20);
  });

  it('uses default pagination values', async () => {
    const mockResult = { data: [], total: 0, page: 1, limit: 20, totalPages: 0 };
    mockListingRepo.findByStatus.mockResolvedValue(mockResult);

    const query = new GetPendingModerationQuery();
    await handler.execute(query);

    expect(mockListingRepo.findByStatus).toHaveBeenCalledWith('PENDING_REVIEW', 1, 20);
  });

  it('passes custom page and limit', async () => {
    const mockResult = { data: [], total: 50, page: 3, limit: 10, totalPages: 5 };
    mockListingRepo.findByStatus.mockResolvedValue(mockResult);

    const query = new GetPendingModerationQuery(3, 10);
    const result = await handler.execute(query);

    expect(result.page).toBe(3);
    expect(result.limit).toBe(10);
    expect(mockListingRepo.findByStatus).toHaveBeenCalledWith('PENDING_REVIEW', 3, 10);
  });
});
