namespace foodmarket.Application.Common;

public sealed class PagedRequest
{
    public int Page { get; init; } = 1;
    public int PageSize { get; init; } = 50;
    public string? Search { get; init; }
    /// <summary>Ключ колонки, по которой сортировать (см. switch в контроллере).</summary>
    public string? Sort { get; init; }
    /// <summary>"asc" (дефолт) или "desc".</summary>
    public string? Order { get; init; }

    public int Skip => Math.Max(0, (Page - 1) * PageSize);
    public int Take => Math.Clamp(PageSize, 1, 500);
    public bool Desc => string.Equals(Order, "desc", StringComparison.OrdinalIgnoreCase);
}

public sealed class PagedResult<T>
{
    public required IReadOnlyList<T> Items { get; init; }
    public required int Total { get; init; }
    public required int Page { get; init; }
    public required int PageSize { get; init; }

    public int TotalPages => PageSize == 0 ? 0 : (int)Math.Ceiling(Total / (double)PageSize);
}
