"""
抽卡详情查询处理模块

处理用户抽卡详情查询，包括最近抽卡记录和成就进度。
"""

from nonebot.adapters.onebot.v11 import Bot, MessageEvent, GroupMessageEvent
import nonebot

from ..utils import get_gacha_system
from .. import formatters

logger = nonebot.logger


async def handle_query(bot: Bot, event: MessageEvent, state: dict):
    """处理抽卡详情查询命令"""
    user_id = str(event.user_id)
    user_name = event.sender.card or event.sender.nickname or "未知用户"
    
    gacha_system = get_gacha_system()
    stats = await gacha_system.get_user_stats(user_id)
    
    if not stats or stats.get("total_draws", 0) == 0:
        await event.finish("您还没有抽卡记录哦~")
    
    # 获取最近抽卡记录
    recent = await gacha_system.get_recent_draws(user_id, limit=5)
    
    # 发送统计详情
    msg = formatters.format_user_detail_stats(stats, user_id, user_name, recent)
    await event.send(msg)
    
    # 发送成就进度
    progress = await gacha_system.get_achievement_progress(user_id)
    if progress:
        achievement_msg = formatters.format_achievement_progress(
            progress.get("consecutive_days", 0),
            progress.get("no_ssr_streak", 0),
            user_id
        )
        await event.send(achievement_msg)
