import  json
import traceback

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 wenxin_process_original_data(data):
    file_path = f'geo/{data["taskId"]}/{data["platform"]}/original.text'
    url_list = ""
    think_content = ""
    response_content = ""
    search_keyword = []
    suggestions = []
    is_think = False
    think_bool = False
    response_bool = False

    try:
        original_content = get_string_from_tos(file_path)

        content_list = original_content.split("\n\n")
        for i in content_list:

            data = i.split("\n")
            event =data[0].split('event:')

            if len(event)>=2:

                if event[1]=='thought':

                    think_content+=json.loads(data[1].split('data:')[1]).get('thoughts')

                if event[1]=='step' :
                    if 'url' in data[1].split('data:')[1]:
                        url_list = json.loads(data[1].split('data:')[1]).get('contents')

                if event[1] == 'state':
                    if json.loads(data[1].split('data:')[1]).get('task_status') == 'SUCCESS':
                        if json.loads(data[1].split('data:')[1]).get('searchCitations'):
                            url_list = json.loads(data[1].split('data:')[1]).get('searchCitations').get("list")
                if event[1]=='message':
                    if data[1].split('data:')[1]:
                        if json.loads(data[1].split('data:')[1]).get('data').get('content'):
                            response_content+=json.loads(data[1].split('data:')[1]).get('data').get('content')
                if event[1] == 'quesRecommend':
                    for i in json.loads(data[1].split('data:')[1]).get('data'):
                        suggestions.append(i.get('guideQuery'))
        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__':
    file_path = 'geo/62069ec4-c957-46b6-940a-230ad5b78a9b/WXYY/original.text'
    wenxin_process_original_data(file_path)


