"""Renderers for user and assistant chat messages."""

from __future__ import annotations

from rich.console import RenderableType
from rich.box import ROUNDED
from rich.panel import Panel
from rich.text import Text

from clients.terminal.tui.themes import get_active_theme

from .base import ContentRenderer


def _hex_style(color) -> str:
    """Return a Rich-compatible hex style string from a Textual Color."""
    return color.hex


class UserMessageRenderer(ContentRenderer):
    """Render a user message bubble."""

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

    def render(self, msg: dict) -> RenderableType:
        theme = get_active_theme()
        text = msg.get("content", "")
        return Panel(
            Text(text, style=_hex_style(theme.text)),
            title="You",
            title_align="left",
            border_style=_hex_style(theme.user_bubble),
            box=ROUNDED,
        )


class AssistantMessageRenderer(ContentRenderer):
    """Render a completed assistant response as a panel."""

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

    def render(self, msg: dict) -> RenderableType:
        theme = get_active_theme()
        text = msg.get("content", "")
        return Panel(
            Text(text, style=_hex_style(theme.text)),
            title="Navi",
            title_align="left",
            border_style=_hex_style(theme.assistant_bubble),
            box=ROUNDED,
        )
