using CMSMicroservice.Domain.Entities;

namespace CMSMicroservice.Application.TransactionsCQ.Queries.GetAllTransactionsByFilter;

public static class TransactionFilterExtensions
{
    public static IQueryable<Transaction> ApplyTransactionFilter(
        this IQueryable<Transaction> query,
        GetAllTransactionsByFilterFilter? filter)
    {
        if (filter == null) return query;

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

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

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

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

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

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

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

        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);

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

        if (filter.PaymentDateTo.HasValue)
            query = query.Where(x => x.PaymentDate <= filter.PaymentDateTo.Value);

        return query;
    }
}
