import { LoginUserCommand } from '../commands/login-user/login-user.command';
import { LoginUserHandler } from '../commands/login-user/login-user.handler';

describe('LoginUserHandler', () => {
  let handler: LoginUserHandler;
  let mockTokenService: { generateTokenPair: ReturnType<typeof vi.fn> };

  const tokenPair = {
    accessToken: 'access-jwt',
    refreshToken: 'family.refresh-hex',
    expiresIn: 900,
  };

  beforeEach(() => {
    mockTokenService = { generateTokenPair: vi.fn().mockResolvedValue(tokenPair) };
    handler = new LoginUserHandler(mockTokenService as any);
  });

  it('generates token pair with correct payload', async () => {
    const command = new LoginUserCommand('user-1', '0912345678', 'BUYER');
    const result = await handler.execute(command);

    expect(result).toEqual(tokenPair);
    expect(mockTokenService.generateTokenPair).toHaveBeenCalledWith({
      sub: 'user-1',
      phone: '0912345678',
      role: 'BUYER',
    });
  });

  it('passes AGENT role correctly', async () => {
    const command = new LoginUserCommand('user-2', '0987654321', 'AGENT');
    await handler.execute(command);

    expect(mockTokenService.generateTokenPair).toHaveBeenCalledWith({
      sub: 'user-2',
      phone: '0987654321',
      role: 'AGENT',
    });
  });

  it('passes ADMIN role correctly', async () => {
    const command = new LoginUserCommand('admin-1', '0901234567', 'ADMIN');
    await handler.execute(command);

    expect(mockTokenService.generateTokenPair).toHaveBeenCalledWith({
      sub: 'admin-1',
      phone: '0901234567',
      role: 'ADMIN',
    });
  });
});
