#
#   Imandra Inc.
#
#   speclogician/presentation/renderers/spec.py
#

from __future__ import annotations

from rich.console import Group
from rich.panel import Panel
from rich.rule import Rule
from rich.table import Table
from rich.text import Text
from rich import box

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.spec import SpecPM


def _scenario_stats_table(pm: SpecPM, *, ctx: RenderCtx) -> Table:
    b = box.MINIMAL if ctx.compact else box.SIMPLE
    t = Table(show_header=False, box=b, show_edge=False, pad_edge=False)
    t.add_column(style="italic dim", no_wrap=True)
    t.add_column(justify="right", no_wrap=True)

    c = pm.counts

    t.add_row(Text("Scenarios", style="bold magenta"), Text(""))
    t.add_row("Total", Text(str(c.num_sc_total), style="bold"))
    t.add_row("Missing components", Text(str(c.num_sc_missing)))
    t.add_row("Matched", Text(str(c.num_sc_matched)))
    t.add_row("Inconsistent", Text(str(c.num_sc_inconsistent)))
    t.add_row("Conflicted", Text(str(c.num_sc_conflicted)))

    return t


def _scenario_list_table(pm: SpecPM, *, ctx: RenderCtx) -> Table:
    b = box.MINIMAL if ctx.compact else box.SIMPLE
    t = Table(title="Scenario list", box=b, show_edge=False, pad_edge=False, expand=False)
    t.add_column("Name", no_wrap=True)

    if not pm.scenario_names:
        t.add_row(Text("(none)", style="dim"))
        return t

    for name in pm.scenario_names:
        t.add_row(Text(name))

    return t


def _scenario_complement_table(pm: SpecPM, *, ctx: RenderCtx) -> Table:
    """
    Lightweight complement summary (counts + optional preview list).
    Full regions are intentionally not rendered here (they can be huge).
    """
    b = box.MINIMAL if ctx.compact else box.SIMPLE
    t = Table(show_header=False, box=b, show_edge=False, pad_edge=False)
    t.add_column(style="italic dim", no_wrap=True)
    t.add_column(justify="right", no_wrap=True)

    sc = getattr(pm, "scenario_complement", None)
    if sc is None:
        t.add_row(Text("Complement", style="bold magenta"), Text(""))
        t.add_row(Text("(none)", style="dim"), Text(""))
        return t

    t.add_row(Text("Complement", style="bold magenta"), Text(""))
    t.add_row("Regions", Text(str(getattr(sc, "num_regions", 0)), style="bold"))

    # Optional “preview” identifiers if you included them in the PM
    preview = list(getattr(sc, "region_fps_preview", []) or [])
    if preview:
        t.add_row(Text("Preview", style="dim"), Text(""))
        for fp in preview[:3]:
            t.add_row("•", Text(str(fp), style="dim"))

    return t


def render_spec(pm: SpecPM, *, ctx: RenderCtx):
    # Domain model block (reuse your existing renderer, but DON'T wrap it in extra panels here)
    from speclogician.presentation.renderers.domain import render_domain_model

    blocks: list[object] = []

    # ---- Domain ----
    blocks.append(Text("Domain", style="bold magenta"))
    blocks.append(render_domain_model(pm.domain, ctx=ctx))

    # ---- Scenarios ----
    blocks.append(Rule(style="dim"))
    blocks.append(Text("Scenarios", style="bold magenta"))
    blocks.append(_scenario_stats_table(pm, ctx=ctx))
    blocks.append(Text(""))  # small spacer
    blocks.append(_scenario_list_table(pm, ctx=ctx))

    # ---- Complement ----
    sc = getattr(pm, "scenario_complement", None)
    if sc is not None or ctx.show_empty_sections:
        blocks.append(Rule(style="dim"))
        blocks.append(_scenario_complement_table(pm, ctx=ctx))

    return Panel(Group(*blocks), title="[bold]Spec[/bold]", border_style="magenta")