import hashlib
import time
from typing import Dict, Any, List, Optional

import httpx

from app.log import logger


# 同步辅助函数（无 await，纯计算）
def safe_get(obj: Optional[Dict], key: str, default: str = "") -> str:
    if obj is None:
        return default
    value = obj.get(key)
    return str(value) if value is not None else default


def safe_join(data, default: str = "") -> str:
    if data is None:
        return default
    if isinstance(data, list):
        return ",".join(str(item) for item in data if item)
    return str(data) if data else default


# 模块级 httpx 单例
_http_client: Optional[httpx.AsyncClient] = None


def get_http_client() -> httpx.AsyncClient:
    global _http_client
    if _http_client is None or _http_client.is_closed:
        _http_client = httpx.AsyncClient(timeout=30.0)
    return _http_client


async def close_http_client() -> None:
    global _http_client
    if _http_client is not None and not _http_client.is_closed:
        await _http_client.aclose()
        _http_client = None


def _build_auth_url() -> str:
    from_id = 9910056
    timestamp = int(time.time())
    salt = "jWcIqJK6QlR2syb6HQgpel9iOoOkj01G5MDFNtQLaTxhddHUTEnURsMe2RxCTYC8"
    token = hashlib.md5((salt + str(timestamp)).encode()).hexdigest()
    return f"http://external-data.qixin.com/extend/extend_data_push?from={from_id}&token={token}&time={timestamp}"


_PUSH_HEADERS = {
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
}


async def push_to_remote(data: Dict[str, Any]) -> bool:
    source_type = data.get("source_type", "未知平台")
    title = data.get("title", "未知职位")
    company = data.get("company_name", data.get("name", "未知公司"))
    logger.info(f"上报数据: [{source_type}] {title} - {company}")
    print(data)

    # try:
    #     url = _build_auth_url()
    #     client = get_http_client()
    #     response = await client.post(url, json=data, headers=_PUSH_HEADERS)
    #     if response.status_code == 200:
    #         return True
    #     logger.error(f"数据发送失败: {response.status_code} - {response.text[:100]}")
    #     return False
    # except Exception as e:
    #     logger.error(f"发送异常: {e}")
    #     return False


async def batch_push_to_remote(data_list: List[Dict[str, Any]]) -> None:
    for data in data_list:
        try:
            await push_to_remote(data)
        except Exception as e:
            logger.error(f"批量推送单条失败: {e}")
