using CMSMicroservice.Domain.Entities;

namespace CMSMicroservice.Application.UserPackagePurchaseCQ.Queries.GetAllUserPackagePurchaseByFilter;

public static class UserPackagePurchaseFilterExtensions
{
    public static IQueryable<UserPackagePurchase> ApplyUserPackagePurchaseFilter(
        this IQueryable<UserPackagePurchase> query,
        GetAllUserPackagePurchaseByFilterFilter? filter)
    {
        if (filter == null) return query;

        if (filter.UserId.HasValue)
            query = query.Where(x => x.UserId == filter.UserId.Value);

        if (filter.PackageId.HasValue)
            query = query.Where(x => x.PackageId == filter.PackageId.Value);

        if (filter.PurchaseMethod.HasValue)
            query = query.Where(x => x.PurchaseMethod == filter.PurchaseMethod.Value);

        if (filter.PurchasedFrom.HasValue)
            query = query.Where(x => x.PurchasedAt >= filter.PurchasedFrom.Value);

        if (filter.PurchasedTo.HasValue)
            query = query.Where(x => x.PurchasedAt <= filter.PurchasedTo.Value);

        if (filter.MinAmount.HasValue)
            query = query.Where(x => x.Amount >= filter.MinAmount.Value);

        if (filter.MaxAmount.HasValue)
            query = query.Where(x => x.Amount <= filter.MaxAmount.Value);

        return query;
    }
}
