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

from __future__ import annotations

from rich.console import Group
from rich.panel import Panel
from rich.text import Text

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.scenario_complement import ScenarioComplementPM


def render_scenario_complement(pm: ScenarioComplementPM, ctx: RenderCtx):
    # Header summary
    hdr = Text()
    hdr.append("Regions: ")
    hdr.append(str(pm.count_regions), style="bold")

    if not pm.regions_preview:
        return Panel(
            Group(hdr, Text("— no regions (or not computed yet) —")),
            title="Scenario Complement",
        )

    lines = []
    for r in pm.regions_preview:
        # One-liner summary
        line = Text()
        line.append(r.fp, style="bold")
        line.append("  ")
        line.append(f"constraints={r.num_constraints}")

        # If we have extra details, add small indented lines
        blocks = [line]
        if r.invariant:
            blocks.append(Text(f"  inv: {r.invariant}"))
        if r.constraints_preview:
            blocks.append(Text(f"  c0: {r.constraints_preview[0]}"))
            for c in r.constraints_preview[1:]:
                blocks.append(Text(f"      {c}"))
        if r.model_eval:
            blocks.append(Text(f"  eval: {r.model_eval}"))
        if r.model:
            blocks.append(Text(f"  model: {r.model}"))

        lines.append(Group(*blocks))

    tail = Text()
    if pm.count_regions > len(pm.regions_preview):
        tail.append(f"(showing {len(pm.regions_preview)} of {pm.count_regions})", style="dim")

    return Panel(
        Group(hdr, *lines, tail),
        title="Scenario Complement",
    )
