import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs';
import { type TokenService, type TokenPair } from '../../../infrastructure/services/token.service';
import { LoginUserCommand } from './login-user.command';

@CommandHandler(LoginUserCommand)
export class LoginUserHandler implements ICommandHandler<LoginUserCommand> {
  constructor(private readonly tokenService: TokenService) {}

  async execute(command: LoginUserCommand): Promise<TokenPair> {
    return this.tokenService.generateTokenPair({
      sub: command.userId,
      phone: command.phone,
      role: command.role,
    });
  }
}
