using Review = Netina.Domain.Entities.Reviews.Review;

namespace Netina.Repository.Handlers.Reviews;

public class GetReviewsQueryHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<GetReviewsQuery, List<ReviewSDto>>
{
    public async Task<List<ReviewSDto>> Handle(GetReviewsQuery request, CancellationToken cancellationToken)
    {
        var query = repositoryWrapper.SetRepository<Review>()
            .TableNoTracking;
        if (request.ProductId != null)
            query = query.Where(q => q.ProductId == request.ProductId);

        return await query
            .OrderByDescending(r => r.CreatedAt)
            .Skip(request.Page * 15)
            .Take(15)
            .Select(ReviewMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
    }
}