import  json

from aidso_geo.utils import robot_utils
from aidso_geo.utils.ai_interface import get_parse_sse_result
from aidso_geo.utils.tos_utils import get_string_from_tos

from aidso_geo.models import  spider_save_tos


def yuanbao_process_original_data(data):
    file_path = f'geo/{data["taskId"]}/{data["platform"]}/original.text'
    url_list = []
    think_content = ""
    response_process = ""
    response_content = ""
    search_keyword = []
    suggestions = []
    is_think = False
    think_bool = False
    response_bool = False
    rich_media_block =[]
    try:
        original_content = get_string_from_tos(file_path)
        content_list = original_content.split("\n\n")

        for i in content_list:

            if i.startswith("data: "):
                try:
                    # 提取并解析JSON数据
                    data_str = i.split("data: ")[1]
                    json_data = json.loads(data_str)
                except (IndexError, json.JSONDecodeError):
                    continue  # 跳过格式错误的数据

                if json_data.get('type') == 'searchGuid':
                    url_list = json_data.get('docs')
                if json_data.get('type') == 'think':
                    think_content+=json_data.get('content')
                if json_data.get('type') == 'text' and json_data.get('msg') is not None:
                    response_content+=json_data.get('msg')
                if json_data.get('type') == 'deepSearch':
                    if json_data.get('contents'):
                        if json_data.get('contents')[0].get('msg'):
                            think_content += json_data.get('contents')[0].get('msg')
                            if not response_process:
                                response_content += json_data.get('contents')[0].get('msg')
                if json_data.get('type') == 'image':
                    response_content = "生成了图片"
        spider_save_tos.process_and_save_files(file_path, search_keyword, url_list, think_content, response_content,
                                               suggestions)
        return (file_path, search_keyword, url_list, think_content, response_content, suggestions)
    except Exception as e:
        parts = file_path.split('/')
        platform = parts[2]
        task_id = parts[1]
        context, quote, suggestion, think, search_word = get_parse_sse_result(platform, task_id)
        if context:
            response_content = context
            url_list = quote
            suggestions = suggestion
            think_content = think
            search_keyword = search_word
            spider_save_tos.process_and_save_files_ai(file_path, search_keyword, url_list, think_content,
                                                      response_content,suggestions)

            return (file_path, search_keyword, url_list, think_content, response_content, suggestions)
        else:
            response_content = "对话信息获取失败:-200"
            robot_utils.feishu_tobot(file_path)

    spider_save_tos.process_and_save_files(file_path, search_keyword, url_list, think_content, response_content,
                                           suggestions)
    return (file_path, search_keyword, url_list, think_content, response_content, suggestions)

if __name__ == '__main__':
    # https://tcdn.aidso.com/geo/c7581554-435b-47ca-a2e5-d52bea763b8a/DOUBA/search_keyword.txt?secret=11049cbfb1c7018597a2085455505304520f0507080c0c4b514e09
    file_path = 'geo/b55be837-48a9-476a-8290-1339ea2ce3f9/TXYB/original.text'
    yuanbao_process_original_data(file_path)

