import { GetAgentByUserIdHandler } from '../queries/get-agent-by-user-id/get-agent-by-user-id.handler';
import { GetAgentByUserIdQuery } from '../queries/get-agent-by-user-id/get-agent-by-user-id.query';

const mockAgent = {
  id: 'agent-1',
  userId: 'user-1',
  licenseNumber: 'LIC-001',
  agency: 'ABC Realty',
  qualityScore: 4.5,
  totalDeals: 12,
  responseTimeAvg: 3600,
  bio: 'Experienced agent in HCMC',
  serviceAreas: ['District 1', 'District 7'],
  isVerified: true,
  createdAt: new Date('2025-01-15'),
};

describe('GetAgentByUserIdHandler', () => {
  let handler: GetAgentByUserIdHandler;
  let mockPrisma: { agent: { findUnique: ReturnType<typeof vi.fn> } };

  beforeEach(() => {
    mockPrisma = {
      agent: { findUnique: vi.fn() },
    };

    handler = new GetAgentByUserIdHandler(mockPrisma as any);
  });

  it('returns agent DTO when agent exists', async () => {
    mockPrisma.agent.findUnique.mockResolvedValue(mockAgent);

    const query = new GetAgentByUserIdQuery('user-1');
    const result = await handler.execute(query);

    expect(result).toEqual({
      id: 'agent-1',
      userId: 'user-1',
      licenseNumber: 'LIC-001',
      agency: 'ABC Realty',
      qualityScore: 4.5,
      totalDeals: 12,
      responseTimeAvg: 3600,
      bio: 'Experienced agent in HCMC',
      serviceAreas: ['District 1', 'District 7'],
      isVerified: true,
      createdAt: new Date('2025-01-15'),
    });
    expect(mockPrisma.agent.findUnique).toHaveBeenCalledWith({
      where: { userId: 'user-1' },
    });
  });

  it('returns null when agent does not exist', async () => {
    mockPrisma.agent.findUnique.mockResolvedValue(null);

    const query = new GetAgentByUserIdQuery('user-without-agent');
    const result = await handler.execute(query);

    expect(result).toBeNull();
  });
});
