using MediatR;
using Modules.Library.Application.Gateways;
using Modules.Rating.Api.Querirs;
using Modules.User.Application;

namespace Modules.Library.Application.Queries.Anime.AnimeTitle;

public class AnimeTitleQuery : IRequest<Models.Anime.Title>
{
    public Guid Id { get; init; }
}

public class AnimeTitleQueryHandler(IAnimeTitleGateway titleGateway, UserContext userContext, IMediator mediator) : IRequestHandler<AnimeTitleQuery, Models.Anime.Title>
{
    public async Task<Models.Anime.Title> Handle(AnimeTitleQuery request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetDetail(request.Id);
        var user = await userContext.GetUserInfo(cancellationToken);
        var rate = await mediator.Send(new ObjectRatingQuery { ObjectId = request.Id, SubjectId = user.Id, }, cancellationToken);
        title.Rate = rate?.ObjectRatePercentage;
        title.MyRate = rate?.SubjectRatePercentage;
        return title;
    }
}