"""Informational block renderers: display-only blocks (no user input collected)."""

from __future__ import annotations

from rich.table import Table
from rich.text import Text

from yera.console.render_result import InformationalResult
from yera.events.models import ActionData, MarkdownData, SpinnerData, TableData


def render_markdown(data: MarkdownData) -> InformationalResult:
    """Markdown (or plain text) content block."""
    if not data.content.strip():
        renderable = Text()
    else:
        renderable = Text(data.content)
    return InformationalResult(renderable=renderable)


def render_action(data: ActionData) -> InformationalResult:
    """Action block: ▶ message when active, ✔ when complete."""
    if data.status == "complete":
        renderable = Text("✔ ", style="green") + Text("Complete")
    else:
        msg = data.message or "Running..."
        renderable = Text("▶ ", style="blue") + Text(msg)
    return InformationalResult(renderable=renderable)


def render_spinner(data: SpinnerData) -> InformationalResult:
    """Spinner block: ⟳ message when active, ✔ when complete."""
    if data.status == "complete":
        renderable = Text("✔ ", style="green") + Text("Complete")
    else:
        msg = data.message or "Loading..."
        renderable = Text("⟳ ", style="blue") + Text(msg)
    return InformationalResult(renderable=renderable)


def _build_table(data: TableData) -> Table:
    """Build a Rich Table from TableData."""
    table = Table(show_header=True, header_style="bold")
    for col in data.columns:
        table.add_column(str(col))
    for row in data.rows:
        table.add_row(*[str(cell) for cell in row])
    return table


def render_table(data: TableData) -> InformationalResult:
    """Table block: renderable is a Rich Table."""
    return InformationalResult(renderable=_build_table(data))
