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

namespace TeleWave.Application.Auth.ChangePassword;

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

        return await identityService.ChangePasswordAsync(
            userId,
            command.CurrentPassword,
            command.NewPassword,
            cancellationToken
        );
    }
}
