"""Tests for planning event renderers."""

from __future__ import annotations

from rich.console import Console

from clients.terminal.tui.renderers.planning import PlanningStatusRenderer, PlanReadyRenderer
from clients.terminal.tui.themes import set_active_theme


def _render_text(renderable) -> str:
    console = Console(record=True, width=80, force_terminal=True, color_system=None)
    console.print(renderable)
    return console.export_text()


def test_planning_status_renderer_accepts_and_renders_label() -> None:
    set_active_theme("gnexus-dark")
    renderer = PlanningStatusRenderer()
    msg = {"type": "planning_status", "phase": 1, "label": "Analysis", "is_subagent": False}
    assert renderer.accepts(msg)
    text = _render_text(renderer.render(msg))
    assert "Planning" in text
    assert "Analysis" in text


def test_planning_status_subagent_prefixed_and_dim() -> None:
    set_active_theme("gnexus-dark")
    renderer = PlanningStatusRenderer()
    msg = {"type": "planning_status", "phase": 2, "label": "Execution plan", "is_subagent": True}
    text = _render_text(renderer.render(msg))
    assert "(subagent)" in text
    assert "Execution plan" in text


def test_plan_ready_renderer_accepts_and_renders_plan() -> None:
    set_active_theme("gnexus-dark")
    renderer = PlanReadyRenderer()
    plan = "## Plan\n\n**Task:** do thing\n**Steps:**\n1. Step one → TOOL: fs\n"
    msg = {"type": "plan_ready", "plan": plan, "is_subagent": False}
    assert renderer.accepts(msg)
    panel = renderer.render(msg)
    assert panel.title == "Plan"
    rendered = _render_text(panel)
    assert "Task" in rendered
    assert "Step one" in rendered


def test_plan_ready_subagent_uses_dim_title() -> None:
    set_active_theme("gnexus-dark")
    renderer = PlanReadyRenderer()
    msg = {"type": "plan_ready", "plan": "## Plan\n**Steps:**\n1. x\n", "is_subagent": True}
    out = _render_text(renderer.render(msg))
    assert "subagent plan" in out