import re
import asyncio
import httpx
import nonebot
from nonebot import on_request
from nonebot.rule import Rule
from nonebot.plugin import PluginMetadata
from nonebot.adapters.onebot.v11.bot import Bot
from nonebot.adapters.onebot.v11.event import GroupRequestEvent
from nonebot.adapters.onebot.v11.message import MessageSegment

__plugin_meta__ = PluginMetadata(
    name='KIRAKIRA☆DOUGA入群验证',
    type='application',
    description='KIRAKIRA☆DOUGA入群验证！',
    homepage='https://github.com/NekoooDesu/nonebot-plugin-okashi',
    usage="通过判断用户入群提供的信息，通过 KiRAKiRA-Rosales 提供的 API 查询用户名验证入群",
    config={},
    extra={}
)

join_group = on_request(
    priority=1,
    block=True
)

async def check_comment(uid: str, invitation_code: str) -> bool:
    config = nonebot.get_driver().config
    uuid = getattr(config, "uuid", None)
    token = getattr(config, "token", None)
    if not uuid or not token:
        return False
    verify_api = f'https://rosales.kirakira.moe/user/getUserByInvitationCode?invitationCode={invitation_code}'
    cookies = {"uuid": uuid, "token": token}
    async with httpx.AsyncClient() as client:
        resp = await client.get(verify_api, cookies=cookies)
        data = resp.json()
        if data.get("success") and "userInfoResult" in data:
            return str(data["userInfoResult"].get("uid")) == uid
        return False

@join_group.handle()
async def _grh(bot: Bot, event: GroupRequestEvent):
    if event.sub_type == 'add':
        comment = event.comment.strip()
        match = re.match(r"(\d+)\s+([A-Za-z0-9\-]+)", comment)
        if match:
            uid, invitation_code = match.groups()
            if await check_comment(uid, invitation_code):
                await event.approve(bot)
                await asyncio.sleep(2)
                await join_group.finish(MessageSegment.at(event.user_id) + f'欢迎加入群聊!\n您的UID: {uid}')
            else:
                await event.reject(bot, reason=f'id: {uid} 认证错误，请检查您输入的是否为正确的UID和邀请码')
        else:
            await event.reject(bot, reason='格式错误，请输入“UID [邀请码]” 如"123 KIRA-1234-1234"')