from io import BytesIO
import base64
import re
from loguru import logger
from nonebot import on_message
from nonebot.rule import Rule

from konabot.common.apis.ali_content_safety import AlibabaGreen
from konabot.common.llm import get_llm
from konabot.common.longtask import DepLongTaskTarget
from konabot.common.nb.extract_image import DepPILImage
from konabot.common.nb.match_keyword import match_keyword


cmd = on_message(rule=Rule(match_keyword(re.compile(r"^千问识图\s*$"))))


@cmd.handle()
async def _(img: DepPILImage, target: DepLongTaskTarget):
    if 1:
        return  #TODO:这里还没写完，还有 Bug 要修
    jpeg_data = BytesIO()
    if img.width > 2160:
        img = img.resize((2160, img.height * 2160 // img.width))
    if img.height > 2160:
        img = img.resize((img.width * 2160 // img.height, 2160))
    img = img.convert("RGB")
    img.save(jpeg_data, format="jpeg", optimize=True, quality=85)
    data_url = "data:image/jpeg;base64,"
    data_url += base64.b64encode(jpeg_data.getvalue()).decode('ascii')

    llm = get_llm("qwen3-vl-plus")
    res = await llm.chat([
        { "role": "user", "content": [
            { "type": "image_url", "image_url": {
                "url": data_url
            } },
            { "type": "text", "text": "请你提取这张图片中的所有文字，并尽量按照原图的排版输出，不需要其他内容" },
        ] }
    ])
    result = res.content
    logger.info(res)
    if result is None:
        await target.send_message("提取失败：可能存在网络异常")
        return

    if not await AlibabaGreen.detect(result):
        await target.send_message("提取失败：图片中可能存在一些不合适的内容")
        return

    await target.send_message(result, at=False)

