"""请求分发 — 按 tool_id 路由到实际运行的服务端点"""

from __future__ import annotations

import logging
from typing import Any, TYPE_CHECKING

import httpx

if TYPE_CHECKING:
    from tool_agent.router.status import ToolStatusManager

logger = logging.getLogger(__name__)


class Dispatcher:
    """根据工具的运行状态分发请求到对应端点"""

    def __init__(self, status_manager: ToolStatusManager) -> None:
        self._status_manager = status_manager

    async def dispatch(self, tool_id: str, params: dict[str, Any], stream: bool = False) -> dict[str, Any]:
        """分发调用请求到工具的活跃端点"""
        # 1. 获取端点信息
        endpoint = self._status_manager.get_active_endpoint(tool_id)
        if not endpoint:
            return {"status": "error", "error": f"Tool '{tool_id}' is not running or has no active endpoint"}

        # 2. 根据端点类型调用
        try:
            async with httpx.AsyncClient(timeout=600.0) as client:
                headers = {}
                if endpoint.get("api_key"):
                    headers["Authorization"] = f"Bearer {endpoint['api_key']}"

                if endpoint["http_method"].upper() == "POST":
                    response = await client.post(endpoint["url"], json=params, headers=headers)
                else:
                    response = await client.get(endpoint["url"], params=params, headers=headers)

                response.raise_for_status()
                return response.json()

        except httpx.HTTPStatusError as e:
            logger.error(f"HTTP error calling '{tool_id}': {e.response.status_code} {e.response.text}")
            return {"status": "error", "error": f"HTTP {e.response.status_code}: {e.response.text}"}
        except Exception as e:
            logger.error(f"Error calling '{tool_id}': {e}")
            return {"status": "error", "error": str(e)}
