"""Tools container - accessed via agent.tools property."""

from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
    from .agent_inventory import AgentInventoryTool
    from .knowledge_graph import KnowledgeGraphTool


class Tools:
    """
    Container for all available tools.

    Accessed via agent.tools property (similar to agent.streamer(message)):
        kg = agent.tools.knowledge_graph
        diseases = await kg.search_diseases(name="Huntington")

    Tools are lazy-initialized - no overhead if not used.
    """

    def __init__(self, gateway_client):
        self._gateway_client = gateway_client
        self._agent_inventory: Optional["AgentInventoryTool"] = None
        self._knowledge_graph: Optional["KnowledgeGraphTool"] = None

    @property
    def agent_inventory(self) -> "AgentInventoryTool":
        """
        Agent Inventory tool for querying active agents and user-accessible agents.

        Example:
            inv = agent.tools.agent_inventory
            agents = await inv.get_active_agents()
            user_agents = await inv.get_user_agents("user-uuid")
        """
        if self._agent_inventory is None:
            from .agent_inventory import AgentInventoryTool

            self._agent_inventory = AgentInventoryTool(
                self._gateway_client.ai_inventory_url
            )
        return self._agent_inventory

    @property
    def knowledge_graph(self) -> "KnowledgeGraphTool":
        """
        Knowledge Graph tool for querying biomedical entities and relationships.

        Example:
            kg = agent.tools.knowledge_graph
            diseases = await kg.search_diseases(name="Huntington")
            similar = await kg.find_similar_diseases("Huntington Disease")
            path = await kg.find_shortest_path("BRCA1", "protein", "Breast Cancer", "disease")
        """
        if self._knowledge_graph is None:
            from .knowledge_graph import KnowledgeGraphTool

            self._knowledge_graph = KnowledgeGraphTool(
                self._gateway_client.knowledge_graph_url
            )
        return self._knowledge_graph

    async def close(self):
        """Close all tool sessions."""
        if self._agent_inventory:
            await self._agent_inventory.close()
        if self._knowledge_graph:
            await self._knowledge_graph.close()
