"""
抽卡排行榜处理模块

处理抽卡排行榜查询命令，支持分页显示。
"""

from nonebot.adapters.onebot.v11 import Bot, MessageEvent, GroupMessageEvent
from nonebot.params import CommandArg
from nonebot.adapters.onebot.v11 import Message
import nonebot

from ..utils import get_gacha_system
from .. import formatters

logger = nonebot.logger


async def handle_rank(bot: Bot, event: MessageEvent, state: dict, args: Message = CommandArg()):
    """处理抽卡排行榜命令"""
    # 解析页码
    page_text = args.extract_plain_text().strip()
    page = 1
    if page_text.isdigit():
        page = int(page_text)
    
    gacha_system = get_gacha_system()
    rank_data, total_pages = await gacha_system.get_rank_list(page=page)
    
    if not rank_data:
        await event.finish("暂无排行数据")
    
    msg = formatters.format_rank_list(rank_data, page, total_pages)
    await event.send(msg)
