"""Agno integration: expose Skytale channels as an Agno toolkit.

Usage::

    from skytale_sdk.integrations import _agno as skytale_agno

    mgr = skytale_agno.create_manager(identity=b"my-agent")
    mgr.create("org/ns/chan")
    agent = Agent(model=OpenAIChat(id="gpt-4.1"),
                  tools=[skytale_agno.toolkit(mgr)])
"""

from __future__ import annotations

from skytale_sdk.channels import SkytaleChannelManager


def create_manager(**kwargs) -> SkytaleChannelManager:
    """Create a ``SkytaleChannelManager`` with the given options.

    Accepts the same keyword arguments as ``SkytaleChannelManager``.

    Returns:
        A configured channel manager.
    """
    return SkytaleChannelManager(**kwargs)


def toolkit(manager: SkytaleChannelManager):
    """Return an Agno ``Toolkit`` bound to *manager*.

    Requires ``agno>=1.0.0``. Install with::

        pip install skytale-sdk[agno]

    Args:
        manager: A ``SkytaleChannelManager`` managing the channels.

    Returns:
        A ``SkytaleToolkit`` instance with ``send``, ``receive``, and
        ``channels`` methods registered as tools.
    """
    from agno.tools import Toolkit

    class SkytaleToolkit(Toolkit):
        def __init__(self):
            super().__init__(name="skytale")
            self._manager = manager
            self.register(self.send)
            self.register(self.receive)
            self.register(self.channels)

        def send(self, channel: str, message: str) -> str:
            """Send an encrypted message to a Skytale channel.

            Args:
                channel: Channel name in org/namespace/service format.
                message: Message text to send.
            """
            self._manager.send(channel, message)
            return f"Message sent to {channel}"

        def receive(self, channel: str, timeout: float = 5.0) -> str:
            """Receive encrypted messages from a Skytale channel.

            Args:
                channel: Channel name in org/namespace/service format.
                timeout: Seconds to wait for messages (default 5).
            """
            msgs = self._manager.receive(channel, timeout=timeout or 5.0)
            if not msgs:
                return f"No new messages on {channel}"
            return "\n".join(f"[{channel}] {m}" for m in msgs)

        def channels(self) -> str:
            """List all active Skytale encrypted channels."""
            chs = self._manager.list_channels()
            if not chs:
                return "No active channels"
            return ", ".join(chs)

    return SkytaleToolkit()
