namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;

/// <summary>
/// One page of the Central UI deployment-status list plus the total row count of
/// the filtered set.
///
/// <para>
/// The count travels WITH the page because the page's pager is a numbered,
/// jump-to-any-page control: it needs a page count, which only a total can give
/// it. That is also the reason this read path is OFFSET-paged rather than keyset
/// -paged like the Audit Log — see
/// <c>IDeploymentManagerRepository.QueryDeploymentListPageAsync</c> for the full
/// rationale.
/// </para>
/// </summary>
/// <param name="Rows">The requested page of rows, newest deployment first.</param>
/// <param name="TotalCount">Total rows matching the filter across all pages.</param>
public sealed record DeploymentListPage(
    IReadOnlyList<DeploymentListRow> Rows,
    int TotalCount)
{
    /// <summary>An empty page — no rows, no matches.</summary>
    public static DeploymentListPage Empty { get; } = new(Array.Empty<DeploymentListRow>(), 0);

    /// <summary>
    /// Number of pages the filtered set spans at the given page size; at least 1
    /// so a pager always has a page to sit on. Returns 1 for a non-positive
    /// <paramref name="pageSize"/> rather than dividing by zero.
    /// </summary>
    /// <param name="pageSize">Rows per page.</param>
    /// <returns>The total page count, never less than 1.</returns>
    public int PageCount(int pageSize) =>
        pageSize <= 0 ? 1 : Math.Max(1, (int)Math.Ceiling(TotalCount / (double)pageSize));
}
