"""
三连抽命令处理模块

处理三连抽命令，包括：
- 三次抽卡执行
- 结果汇总
- 成就检查
"""

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

from ..utils import get_gacha_system
from .. import formatters
from ..utils import build_achievement_notify

logger = nonebot.logger


async def handle_triple_gacha(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()
    results = []
    
    # 执行三次抽卡
    for _ in range(3):
        result = await gacha_system.draw(user_id)
        if result["success"]:
            results.append((result["rarity"], result["name"], result["image"]))
        else:
            await event.finish(result["message"])
    
    # 发送三连抽结果
    msg = formatters.format_triple_gacha_result(results, user_id, user_name)
    await event.send(msg)
    
    # 成就检查（使用统一编排函数，避免接口不匹配）
    unlocked = await gacha_system.check_achievements(user_id)
    if unlocked:
        achievement_msg = await build_achievement_notify(user_id, unlocked)
        if achievement_msg:
            await event.send(achievement_msg)
