using MediatR;
using Modules.Library.Application.Models;

namespace Modules.Library.Application.Queries.Anime;

public class GetMediaQuery : IRequest<ObjectResult?>
{
    public string ObjectId { get; set; } = default!;
}

public class GetMediaQueryHandler(IMediator mediator) : IRequestHandler<GetMediaQuery, ObjectResult?>
{
    public async Task<ObjectResult?> Handle(GetMediaQuery request, CancellationToken cancellationToken)
    {
        //var userInfo = await userContext.GetUserInfo(cancellationToken);
        //if (userInfo?.AccountId == null) throw new Exception("User not found");
        //var user = await userGateway.Get(userInfo.AccountId.Value) ?? throw new Exception("User was not found");
        if (string.IsNullOrWhiteSpace(request.ObjectId))
        {
            return null;
        }
        else
        {
            var result = await mediator.Send(new Media.Api.Queries.GetObjectQuery
            {
                BusketName = "my-bookmark.library",
                ObjectName = request.ObjectId,
            }, cancellationToken);
            return result == null ? null : new ObjectResult
            {
                ObjectName = result.ObjectName,
                ContentType = result.ContentType,
                Data = result.Data,
            };
        }
    }
}