from embed import embed
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

async def handle(params_kw):
    logger.debug(f'{params_kw=}')
    
    # 验证文件格式
    supported_formats = {'pdf', 'docx', 'xlsx', 'pptx', 'csv', 'txt'}
    file_path = params_kw.file_path
    if not file_path or '.' not in file_path or file_path.rsplit('.', 1)[1].lower() not in supported_formats:
        return {
            "widgettype": "Error",
            "options": {
                "archor": "cc",
                "timeout": 5,
                "auto_open": True,
                "auto_dismiss": True,
                "auto_destroy": True,
                "cwidth": 23,
                "cheight": 17,
                "title": "Error",
                "message": f"文件格式不支持，支持的格式：{', '.join(supported_formats)}"
            }
        }

    # 调用 embed 函数
    success = await embed(file_path, params_kw.userid, params_kw.db_type)
    if success:
        return {
            "widgettype": "Message",
            "options": {
                "archor": "cc",
                "timeout": 5,
                "auto_open": True,
                "auto_dismiss": True,
                "auto_destroy": True,
                "cwidth": 23,
                "cheight": 17,
                "title": "Success",
                "message": "文件添加成功"
            }
        }
    return {
        "widgettype": "Error",
        "options": {
            "archor": "cc",
            "timeout": 5,
            "auto_open": True,
            "auto_dismiss": True,
            "auto_destroy": True,
            "cwidth": 23,
            "cheight": 17,
            "title": "Error",
            "message": "文件添加失败"
        }
    }