"""Renderer for the context-compression summary block."""

from __future__ import annotations

from rich.box import ROUNDED
from rich.console import RenderableType
from rich.markdown import Markdown
from rich.panel import Panel

from clients.terminal.tui.themes import get_active_theme

from .base import ContentRenderer
from .markdown_content import ThemedMarkdownRenderable, _theme_aware_code_theme


class ContextSummaryRenderer(ContentRenderer):
    """Render the context-compression summary as a bordered card.

    The compressor discards old turns and replaces them with a summary message;
    this renderer shows that summary text (what was kept) with a header giving
    the before/after message counts. Rendered for both forced /compact and
    mid-turn auto-compress, so the user can always see what was summarized.
    """

    def accepts(self, msg: dict) -> bool:
        return msg.get("type") == "context_summary"

    def render(self, msg: dict) -> RenderableType:
        theme = get_active_theme()
        before = msg.get("messages_before")
        after = msg.get("messages_after")
        title = "Context compressed"
        if before is not None and after is not None:
            title = f"Context compressed: {before} → {after} messages"
        code_theme = _theme_aware_code_theme(theme.name)
        body = ThemedMarkdownRenderable(
            Markdown(msg.get("content", "") or "", code_theme=code_theme, inline_code_theme=code_theme),
            theme.name,
        )
        return Panel(
            body,
            title=title,
            title_align="left",
            border_style=theme.text_dim.hex,
            box=ROUNDED,
        )