using MediatR;
using Modules.User.Application.Models;
using Modules.User.Application.Repositories;

namespace Modules.User.Application.Queries;

public class GetUserAvatarQuery : IRequest<ObjectResult?>
{
    public Guid UserId { get; init; }
}

public class GetUserAvatarQueryHandler(IUserQueries userGateway, IMediator mediator) : IRequestHandler<GetUserAvatarQuery, ObjectResult?>
{
    public async Task<ObjectResult?> Handle(GetUserAvatarQuery request, CancellationToken cancellationToken)
    {
        var avatarId = await userGateway.GetAvatarIdAsync(request.UserId, cancellationToken);
        if (string.IsNullOrWhiteSpace(avatarId)) return null;
        
        var result = await mediator.Send(new Media.Api.Queries.GetObjectQuery
        {
            BusketName = "my-bookmark.user",
            ObjectName = avatarId,
        }, cancellationToken);
        return result == null ? null : new ObjectResult
        {
            ObjectName = result.ObjectName,
            ContentType = result.ContentType,
            Data = result.Data,
        };
    }
}