# -*- coding: utf-8 -*-
import requests
import json
import time
import requests

from aidso_geo.utils.robot_utils import feishu_tobot_sse


def get_doubao_message(prop):
    url = "https://ark.cn-beijing.volces.com/api/v3/responses"

    payload = json.dumps({
        "model": "doubao-seed-1-6-251015",
        "stream": False,
        "tools": [
            {
                "type": "web_search",
                "max_keyword": 3
            }
        ],
        "input": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "input_text",
                        "text": prop
                    }
                ]
            }
        ],
        "thinking": {
            "type": "disabled"
        }
    })
    headers = {
        'Authorization': 'Bearer fcc424e5-58af-494d-9683-5787413a26c9',
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    response_json = response.json()
    text = ""
    quto = ""
    for i in response_json.get('output'):
        if i.get('type') == 'message':
            for j in i.get('content'):
                # j_json = json.loads(j)
                text = j.get('text')
                quto = j.get('annotations')

    return (text,quto)


def get_kimi_message(prop):
    url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"

    payload = json.dumps({
       "model": "kimi-k2-250905",
       "messages": [
          {
             "role": "system",
             "content": "你是人工智能助手."
          },
          {
             "role": "user",
             "content": prop
          }
       ]
    })
    headers = {
       'Authorization': 'Bearer fcc424e5-58af-494d-9683-5787413a26c9',
       'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get('choices')[0].get('message').get('content')


def get_deepseek_message(prop):
    url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"

    payload = json.dumps({
       "model": "deepseek-v3-1-terminus",
       "messages": [
          {
             "role": "system",
             "content": "你是人工智能助手."
          },
          {
             "role": "user",
             "content": prop
          }
       ]
    })
    headers = {
       'Authorization': 'Bearer fcc424e5-58af-494d-9683-5787413a26c9',
       'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get('choices')[0].get('message').get('content')



def get_qianwewn_message(prop):

    url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"

    payload = json.dumps({
        "model": "qwen-plus",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": prop
            }
        ],
        "enable_search": True
    })
    headers = {
        'Authorization': 'Bearer sk-eed87fdb2c8e42d79353ba345db83d0a',
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    return response.json().get('choices')[0].get('message').get('content')



MAX_POLL = 5
POLL_INTERVAL_SEC = 30

def get_parse_sse(platform, task_id):
    url = f"http://115.191.66.107:8894/parse_sse?platform={platform}&sse_url=https://douchacha-web.tos-cn-beijing.volces.com/geo/{task_id}/{platform}/original.text"
    payload = {}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)
    feishu_tobot_sse(url + "    触发sse解析接口")
    return response.json()


def get_parse_sse_result(platform, task_id):
    response_data = get_parse_sse(platform, task_id)

    if not response_data.get("ok"):
        return ("", [], [], "",[])
    else:
        for i in range(MAX_POLL):
            code = response_data.get("code")
            data = response_data.get("data")
            if code == 200:
                context = data.get('context')
                quote = data.get('quote')
                suggestions = data.get('suggestions')
                think = data.get('think')
                search_word = data.get('search_word')
                if len(think) < 30:
                    think = ""
                return (context, quote, suggestions, think, search_word)
            if i == MAX_POLL - 1:
                return ("", [], [], "",[])

            time.sleep(POLL_INTERVAL_SEC)

# 元宝
# 文心一言
if __name__ == '__main__':

    text,quto = get_doubao_message("GEO服务商推荐")
    print(quto)
