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

namespace Modules.Library.Application.Queries.Anime;

public class AnimeEpisodeListShortQuery : IRequest<List<Models.Anime.Title>>
{
}

public class AnimeTitleListQueryHandler(IAnimeTitleGateway titleGateway, UserContext userContext, IMediator mediator) : IRequestHandler<AnimeTitleListQuery, List<Models.Anime.Title>>
{
    public async Task<List<Models.Anime.Title>> Handle(AnimeTitleListQuery request, CancellationToken cancellationToken)
    {
        var titles = await titleGateway.GetList();
        var user = await userContext.GetUserInfo(cancellationToken);
        var rates = await mediator.Send(new ObjectRatingListQuery { ObjectIds = titles.Select(q => q.Id), SubjectId = user.Id, }, cancellationToken);
        rates.ForEach(q =>
        {
            var title = titles.First(x => x.Id == q.ObjectId);
            title.Rate = q.ObjectRatePercentage;
            title.MyRate = q.SubjectRatePercentage;
        });
        return titles;
    }
}