import { GetUserDetailHandler } from '../queries/get-user-detail/get-user-detail.handler';
import { GetUserDetailQuery } from '../queries/get-user-detail/get-user-detail.query';

describe('GetUserDetailHandler', () => {
  let handler: GetUserDetailHandler;
  let mockRepo: { getUserDetail: ReturnType<typeof vi.fn> };

  beforeEach(() => {
    mockRepo = {
      getUserDetail: vi.fn(),
    };
    handler = new GetUserDetailHandler(mockRepo as any);
  });

  it('returns user detail when found', async () => {
    const userDetail = {
      id: 'u1',
      email: 'test@test.com',
      phone: '0901234567',
      fullName: 'Test User',
      avatarUrl: null,
      role: 'BUYER',
      kycStatus: 'NONE',
      kycData: null,
      isActive: true,
      createdAt: new Date(),
      updatedAt: new Date(),
      listingsCount: 5,
      activeListingsCount: 2,
      transactionsCount: 3,
      subscription: null,
      recentActivity: [],
    };
    mockRepo.getUserDetail.mockResolvedValue(userDetail);

    const query = new GetUserDetailQuery('u1');
    const result = await handler.execute(query);

    expect(result).toEqual(userDetail);
    expect(mockRepo.getUserDetail).toHaveBeenCalledWith('u1');
  });

  it('throws NotFoundException when user not found', async () => {
    mockRepo.getUserDetail.mockResolvedValue(null);

    const query = new GetUserDetailQuery('nonexistent');
    await expect(handler.execute(query)).rejects.toThrow('Người dùng không tồn tại');
  });
});
