"""Transport-agnostic output handler protocol for Agent Johnny 5.

Decouples the agent brain from any specific UI framework (Textual TUI,
Telegram bot, HTTP API, etc.). Each frontend implements this protocol
to receive agent output.
"""

from __future__ import annotations

import logging
from typing import Protocol, runtime_checkable


@runtime_checkable
class OutputHandler(Protocol):
    """Interface for delivering agent output to any frontend."""

    def on_typing(self) -> None:
        """Agent is processing (show typing indicator)."""
        ...

    def on_reply(self, text: str) -> None:
        """Agent produced a text response."""
        ...

    def on_tool_call(self, tool_name: str, args: dict) -> None:
        """Agent invoked a tool (for display/logging)."""
        ...

    def on_error(self, error: str) -> None:
        """Agent encountered an error."""
        ...


class LoggingOutputHandler:
    """Fallback handler that logs output (used in daemon/headless mode)."""

    def __init__(self) -> None:
        self._logger = logging.getLogger("easyclaw.agent.output")

    def on_typing(self) -> None:
        pass

    def on_reply(self, text: str) -> None:
        self._logger.info("Agent: %s", text[:200])

    def on_tool_call(self, tool_name: str, args: dict) -> None:
        self._logger.info("Tool: %s(%s)", tool_name, args)

    def on_error(self, error: str) -> None:
        self._logger.error("Agent error: %s", error)
