"""
{{ display_name }} - Summarize node.

This module handles content summarization using LLM.
"""

from torivers_sdk.tools import LLMClient, LLMMessage

from state import {{ class_name }}State

SUMMARIZE_PROMPT = """Summarize the following content concisely.
Focus on the main points and key information.

Content:
{content}

Provide a clear, well-structured summary."""


async def summarize(state: {{ class_name }}State) -> dict:
    """Generate a summary of the input content.

    Args:
        state: Current analysis state.

    Returns:
        Dictionary with state updates including summary.
    """
    context = state["context"]
    input_data = state["input_data"]

    content = input_data.get("content", "")
    context.log_progress("Summarizing", "Generating content summary...")

    llm = LLMClient.get_default()
    response = await llm.chat(
        messages=[
            LLMMessage(
                role="system",
                content="You are an expert content analyst. Provide clear, concise summaries.",
            ),
            LLMMessage(
                role="user",
                content=SUMMARIZE_PROMPT.format(content=content),
            ),
        ],
    )

    context.log_success("Summary generated", f"Used {response.tokens_used} tokens")

    return {
        "current_step": "summarized",
        "summary": response.content,
        "total_tokens_used": response.tokens_used,
        "metadata": {
            "summary_tokens": response.tokens_used,
        },
    }
