namespace CMSMicroservice.Application.ProductsCQ.Queries.GetAllProductsByFilter;
public class GetAllProductsByFilterQueryHandler : IRequestHandler<GetAllProductsByFilterQuery, GetAllProductsByFilterResponseDto>
{
    private readonly IApplicationDbContext _context;

    public GetAllProductsByFilterQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<GetAllProductsByFilterResponseDto> Handle(GetAllProductsByFilterQuery request, CancellationToken cancellationToken)
    {
        var query = _context.Products
            .ApplyOrder(sortBy: request.SortBy)
            .AsNoTracking()
            .AsQueryable();
        if (request.Filter is not null)
        {
            query = query
                .Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
                .Where(x => request.Filter.Title == null || x.Title.Contains(request.Filter.Title))
                .Where(x => request.Filter.Description == null || x.Description.Contains(request.Filter.Description))
                .Where(x => request.Filter.ShortInfomation == null || x.ShortInfomation.Contains(request.Filter.ShortInfomation))
                .Where(x => request.Filter.FullInformation == null || x.FullInformation.Contains(request.Filter.FullInformation))
                .Where(x => request.Filter.Price == null || x.Price == request.Filter.Price)
                .Where(x => request.Filter.Discount == null || x.Discount == request.Filter.Discount)
                .Where(x => request.Filter.Rate == null || x.Rate == request.Filter.Rate)
                .Where(x => request.Filter.CategoryId == null || x.ProductCategories.Any(pc => pc.CategoryId == request.Filter.CategoryId))
                .Where(x => request.Filter.ImagePath == null || x.ImagePath.Contains(request.Filter.ImagePath))
                .Where(x => request.Filter.ThumbnailPath == null || x.ThumbnailPath.Contains(request.Filter.ThumbnailPath))
                .Where(x => request.Filter.SaleCount == null || x.SaleCount == request.Filter.SaleCount)
                .Where(x => request.Filter.ViewCount == null || x.ViewCount == request.Filter.ViewCount)
                .Where(x => request.Filter.RemainingCount == null || x.RemainingCount == request.Filter.RemainingCount)
;
        }
        var meta = await query.GetMetaData(request.PaginationState, cancellationToken);

        var models = await query
            .PaginatedListAsync(paginationState: request.PaginationState)
            .Select(x => new GetAllProductsByFilterResponseModel
            {
                Id = x.Id,
                Title = x.Title,
                Description = x.Description,
                ShortInfomation = x.ShortInfomation,
                FullInformation = x.FullInformation,
                Price = x.Price,
                Discount = x.Discount,
                Rate = x.Rate,
                ImagePath = x.ImagePath,
                ThumbnailPath = x.ThumbnailPath,
                SaleCount = x.SaleCount,
                ViewCount = x.ViewCount,
                RemainingCount = x.RemainingCount,
                CategoryIds = x.ProductCategories
                    .Select(pc => pc.CategoryId)
                    .ToList()
            })
            .ToListAsync(cancellationToken);

        return new GetAllProductsByFilterResponseDto
        {
            MetaData = meta,
            Models = models
        };        
    }
}
