using Baya.Application.Contracts;
using Baya.Application.Models.Common;
using Baya.Application.Models.Jwt;
using Mediator;

namespace Baya.Application.Features.Users.Commands.RefreshUserTokenCommand
{
    internal class RefreshUserTokenCommandHandler(IJwtService jwtService)
        : IRequestHandler<RefreshUserTokenCommand, OperationResult<AccessToken>>
    {
        public async ValueTask<OperationResult<AccessToken>> Handle(RefreshUserTokenCommand request, CancellationToken cancellationToken)
        {
            var newToken = await jwtService.RefreshToken(request.RefreshToken);

            if(newToken is null)
                return OperationResult<AccessToken>.FailureResult("Invalid refresh token");

            return OperationResult<AccessToken>.SuccessResult(newToken);
        }
    }
}
