#
#   Imandra Inc.
#
#   speclogician/tui/demo.py
#

from __future__ import annotations

from typing import Any
from textual.screen import Screen
from textual.app import ComposeResult
from textual.widgets import Header, Footer, RichLog

from rich.markdown import Markdown
from rich.panel import Panel


class DemoScreen(Screen[Any]):
    """Shows demo collateral (artifact markdown) before entering the main TUI."""

    CSS = """
    #mdlog { height: 1fr; width: 100%; }
    """

    def __init__(self, markdown_text: str, *, title: str = "Demo artifacts") -> None:
        super().__init__()
        self._md_text = markdown_text
        self._title = title

    def compose(self) -> ComposeResult:
        yield Header()
        self.mdlog = RichLog(id="mdlog", wrap=True, highlight=True, markup=False, auto_scroll=False)
        yield self.mdlog
        yield Footer()

    def on_mount(self) -> None:
        # Render markdown inside a panel and write it as a single renderable.
        self.mdlog.clear()
        self.mdlog.write(
            Panel(
                Markdown(self._md_text or "_(empty)_"),
                title=f"[bold]{self._title}[/bold]",
                border_style="magenta",
            )
        )
