from __future__ import annotations

from typing import Annotated, Any

from fastapi import Query

from app.services.pagination import PageResult

PageNumber = Annotated[int | None, Query(ge=1, description="页码，从 1 开始。")]
PageSize = Annotated[int | None, Query(ge=1, le=100, description="每页条数，最多 100。")]


def wants_page(page: int | None, page_size: int | None) -> bool:
    return page is not None or page_size is not None


def page_payload(result: PageResult[Any]) -> dict[str, Any]:
    return {
        "items": result.items,
        "total": result.total,
        "page": result.page,
        "page_size": result.page_size,
        "total_pages": result.total_pages,
        "has_next": result.has_next,
        "has_previous": result.has_previous,
    }
