import requests
import os, sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from aidso_geo.config.base_config import PlatformType, BaseConfig
from aidso_geo.utils.tos_utils import put_string_to_tos


def get_platform_response(
        data
) -> bool:
    max_retries = 3
    retry_count = 0

    try:
        platform_enum = PlatformType.from_str(data["platform"])
    except ValueError as e:
        return False

    platform_key = platform_enum.value
    platform_config = BaseConfig.PLATFORM_CONFIGS.get(platform_key)
    if not platform_config:
        return False
    params = {"message": data['prompt']}
    params["search_enabled"] = data.get('searchEnabled', '1')
    if data.get('thinkingEnabled'):
        params["thinking_enabled"] = data.get('thinkingEnabled')


    while retry_count < max_retries:
        try:
            response = requests.get(platform_config["url"], params=params, timeout=600)
            response_data = response.json()
            storage_path = platform_config["storage_path"](data['taskId'])
            if response_data.get("code") == 200:
                put_string_to_tos(storage_path, response_data.get("data"))
                return True
            elif response_data.get("code") == 400:
                retry_count += 1
                if retry_count >= max_retries:
                    return False
        except Exception as e:
            return False



if __name__ == '__main__':
        data = {
            "prompt": "羽绒服品牌推荐？",
            "taskId": "3fa90552-d3c7-4c26-aaf6-6722a213a240",
            "reqId": "aaaaaaAwwww069i",
            "platform": "TXYBA",
            "type": "stream",
        }

        print(get_platform_response(data))


