using CMSMicroservice.Domain.Entities.History;

namespace CMSMicroservice.Application.ClubMembershipCycleHistoryCQ.Queries.GetAllClubMembershipCycleHistoryByFilter;

public static class ClubMembershipCycleHistoryFilterExtensions
{
    public static IQueryable<ClubMembershipCycleHistory> ApplyClubMembershipCycleHistoryFilter(
        this IQueryable<ClubMembershipCycleHistory> query,
        GetAllClubMembershipCycleHistoryByFilterFilter? filter)
    {
        if (filter == null) return query;

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

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

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

        if (filter.Action.HasValue)
            query = query.Where(x => x.Action == filter.Action.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);

        return query;
    }
}
