"""Ranking service for user leaderboard."""

from database import get_top_users, get_user_rank


async def get_leaderboard(page: int = 1, page_size: int = 20) -> list[dict]:
    """Get leaderboard with user rankings.

    Returns list of users with their rank and score.
    """
    top_users = await get_top_users(page_size)
    leaderboard = []

    for idx, user in enumerate(top_users, start=1):
        leaderboard.append({
            "rank": idx,
            "full_name": user.get("full_name", ""),
            "score": user.get("score", 0)
        })

    return leaderboard


async def get_user_position(user_id: int) -> dict:
    """Get the position of a specific user in the leaderboard.

    Returns dict with rank, full_name, and score.
    """
    from database import get_user_by_telegram_id

    user = await get_user_by_telegram_id(user_id)
    if not user:
        return {}

    rank = await get_user_rank(user["id"])

    return {
        "rank": rank,
        "full_name": user.get("full_name", ""),
        "score": user.get("score", 0)
    }