using CMSMicroservice.Domain.Entities.Payment;

namespace CMSMicroservice.Application.PaymentTransactionCQ.Queries.GetAllPaymentTransactionByFilter;

public static class PaymentTransactionFilterExtensions
{
    public static IQueryable<PaymentTransaction> ApplyPaymentTransactionFilter(
        this IQueryable<PaymentTransaction> query,
        GetAllPaymentTransactionByFilterFilter? filter)
    {
        if (filter == null) return query;

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

        if (!string.IsNullOrWhiteSpace(filter.Mobile))
            query = query.Where(x => x.Mobile != null && x.Mobile.Contains(filter.Mobile));

        if (!string.IsNullOrWhiteSpace(filter.GatewayProvider))
            query = query.Where(x => x.GatewayProvider == filter.GatewayProvider);

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

        if (!string.IsNullOrWhiteSpace(filter.Authority))
            query = query.Where(x => x.Authority != null && x.Authority.Contains(filter.Authority));

        if (!string.IsNullOrWhiteSpace(filter.RefId))
            query = query.Where(x => x.RefId != null && x.RefId.Contains(filter.RefId));

        if (!string.IsNullOrWhiteSpace(filter.OrderId))
            query = query.Where(x => x.OrderId != null && x.OrderId.Contains(filter.OrderId));

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

        if (filter.CreatedTo.HasValue)
            query = query.Where(x => x.Created <= filter.CreatedTo.Value);

        return query;
    }
}
