import os


async def dispatch_assign_once_flow(client):
    """
    调用新的一键派单接口：
    GET {DISPATCH_BASE_URL}/assign
    Header: X-API-Key
    """
    base_url = os.getenv("DISPATCH_BASE_URL", "http://1.12.50.92:8006").strip().rstrip("/")
    api_key = os.getenv("DISPATCH_API_KEY", "tuhui_dispatch_key_2026").strip()
    timeout_s = float(os.getenv("DISPATCH_TIMEOUT_SECONDS", "5"))
    if not base_url or not api_key:
        return {"success": False, "reason": "dispatch config missing"}
    try:
        import httpx

        async with httpx.AsyncClient(timeout=timeout_s) as http_client:
            resp = await http_client.get(
                f"{base_url}/assign",
                headers={"X-API-Key": api_key},
            )
        if resp.status_code != 200:
            return {"success": False, "reason": f"http {resp.status_code}"}
        data = resp.json() if resp.content else {}
        ok = bool((data or {}).get("success", False))
        return {
            "success": ok,
            "task_id": str((data or {}).get("task_id", "") or ""),
            "assigned_to": str((data or {}).get("assigned_to", "") or ""),
            "online_count": int((data or {}).get("online_count", 0) or 0),
            "notification_sent": bool((data or {}).get("notification_sent", False)),
            "raw": data,
        }
    except Exception as e:
        return {"success": False, "reason": str(e)}
