namespace adas_core.Domain.Models.Filter;

public record PaginationFilter
{
    public PaginationFilter()
    {
    }

    public PaginationFilter(int pageNumber, int pageSize, FilteredRequest? filtered)
    {
        PageNumber = pageNumber < 1 ? 1 : pageNumber;
        PageSize = pageSize <= 0 ? 100 : pageSize;
        FilteredRequest = filtered;
    }

    public int PageNumber { get; set; } = 1;
    public int PageSize { get; set; } = 100;
    public FilteredRequest? FilteredRequest { get; set; }

    public bool IsEmpty()
    {
        return PageNumber <= 0 && PageSize <= 0;
    }
}

public static class PaginationFilterExtensions
{
    public static IQueryable<T> ApplyPagination<T>(this IQueryable<T> queryable, PaginationFilter? filter)
    {
        var modifQueryable = queryable;
        if (filter is not { PageNumber: >= 0, PageSize: > 0 }) return modifQueryable;
        var pageNumber = filter.PageNumber;
        var pageSize = filter.PageSize;
        modifQueryable = modifQueryable
                .Skip((pageNumber - 1) * pageSize)
                .Take(pageSize)
            ;

        return modifQueryable;
    }
}