"""Built-in tool middleware — logs every tool call for observability."""

import structlog

from navi.tools._internal.middleware import ToolMiddleware
from navi.tools._internal.base import ToolResult

log = structlog.get_logger()


class LoggingMiddleware(ToolMiddleware):
    """Logs every tool execution with duration and result summary."""

    async def before_execute(self, tool_name: str, params: dict) -> None:
        log.debug("middleware.tool.before", tool=tool_name, args=params)

    async def after_execute(self, tool_name: str, params: dict, result: ToolResult) -> None:
        log.info(
            "middleware.tool.after",
            tool=tool_name,
            success=result.success,
            output_len=len(result.output),
            has_error=bool(result.error),
        )
