"""
Pagination utilities.
"""

from typing import List, Any, Tuple, Optional

from fastapi import Request

from ..constants import DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE


def get_pagination_params(request: Request) -> Tuple[int, int]:
    """
    Extract pagination parameters from request query string.

    Args:
        request: FastAPI request

    Returns:
        Tuple of (page, limit)
    """
    try:
        page = int(request.query_params.get("page", 1))
        page = max(1, page)
    except ValueError:
        page = 1

    try:
        limit = int(request.query_params.get("limit", DEFAULT_PAGE_SIZE))
        limit = max(1, min(limit, MAX_PAGE_SIZE))
    except ValueError:
        limit = DEFAULT_PAGE_SIZE

    return page, limit


def paginate(
    items: List[Any],
    page: int = 1,
    limit: int = DEFAULT_PAGE_SIZE,
) -> Tuple[List[Any], dict]:
    """
    Paginate a list of items.

    Args:
        items: Full list of items
        page: Page number (1-indexed)
        limit: Items per page

    Returns:
        Tuple of (paginated items, pagination info dict)
    """
    total = len(items)
    start = (page - 1) * limit
    end = start + limit

    paginated = items[start:end]

    return paginated, {
        "page": page,
        "limit": limit,
        "total": total,
        "has_more": end < total,
        "total_pages": (total + limit - 1) // limit if total > 0 else 1,
    }


def calculate_offset(page: int, limit: int) -> int:
    """Calculate database offset from page and limit."""
    return (page - 1) * limit


def build_pagination_info(
    page: int,
    limit: int,
    total: int,
) -> dict:
    """Build pagination info dictionary."""
    return {
        "page": page,
        "limit": limit,
        "total": total,
        "has_more": page * limit < total,
        "total_pages": (total + limit - 1) // limit if total > 0 else 1,
    }
