using LiteCqrs;
using TeleWave.Application.Common.Interfaces;
using TeleWave.Application.Common.Models;

namespace TeleWave.Application.Auth.ChangeUserName;

public sealed class ChangeUserNameCommandHandler(
    IIdentityService identityService,
    ICurrentUser currentUser
) : ICommandHandler<ChangeUserNameCommand, Result>
{
    public async Task<Result> Handle(
        ChangeUserNameCommand command,
        CancellationToken cancellationToken
    )
    {
        if (currentUser.UserId is not { } userId)
            return Result.Failure(AuthErrors.Unauthorized);

        return await identityService.ChangeUserNameAsync(
            userId,
            command.NewUserName,
            cancellationToken
        );
    }
}
