"""Search tool."""

from __future__ import annotations

from penguiflow.catalog import tool
from penguiflow.planner import ToolContext

from ..models import Query, SearchResult, SearchResults


@tool(desc="Search internal knowledge sources", side_effects="read", tags=["planner"])
async def search_documents(args: Query, ctx: ToolContext) -> SearchResults:
    tenant = ctx.tool_context.get("tenant_id", "public")
{% if with_streaming %}
    await ctx.emit_chunk("search", 0, f"Searching {tenant} sources for '{args.question}'...")
    # Publish status update if callback is available
    status_publisher = ctx.tool_context.get("status_publisher")
    if status_publisher:
        status_publisher({"status": "searching", "tenant": tenant, "question": args.question})
{% endif %}
    snippet = f"Result for '{args.question}' in tenant '{tenant}'"
{% if with_streaming %}
    await ctx.emit_chunk("search", 1, "Found 1 result", done=True)
    if status_publisher:
        status_publisher({"status": "found", "results": 1})
{% endif %}
    return SearchResults(results=[SearchResult(title="stub", snippet=snippet)])
