#
#   Imandra Inc.
#
#   presentation/renderers/state_instance_summary.py
#

from __future__ import annotations

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

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.state_instance_summary import StateInstanceSummaryPM


def render_state_instance_summary(pm: StateInstanceSummaryPM, *, ctx: RenderCtx):
    def kv(k: str, v: object, *, k_w: int = 22) -> Text:
        # left-align key, keep values readable; no fancy tables
        return Text(f"{k:<{k_w}} {v}")

    blocks: list[Text] = []

    # Header
    blocks.append(Text("State instance", style="bold"))
    blocks.append(Rule(style="dim"))

    # Top-level
    blocks.append(kv("created_at:", pm.created_at))
    blocks.append(kv("changes:", pm.num_changes))
    blocks.append(Text(""))

    # Domain model
    blocks.append(Text("Domain model", style="bold"))
    blocks.append(kv("base_status:", pm.base_status))
    blocks.append(kv("has_state/action:", f"{pm.base_has_state} / {pm.base_has_action}"))

    blocks.append(
        kv(
            "state preds:",
            f"{pm.num_state_preds_total} total, "
            f"{pm.num_state_preds_matched} matched, "
            f"{pm.num_state_preds_valid_logic} logic-valid, "
            f"{pm.num_state_preds_errored} errored",
        )
    )
    blocks.append(
        kv(
            "action preds:",
            f"{pm.num_action_preds_total} total, "
            f"{pm.num_action_preds_matched} matched, "
            f"{pm.num_action_preds_valid_logic} logic-valid, "
            f"{pm.num_action_preds_errored} errored",
        )
    )
    blocks.append(
        kv(
            "predicates:",
            f"{pm.num_preds_total} total, "
            f"{pm.num_preds_matched} matched, "
            f"{pm.num_preds_valid_logic} logic-valid, "
            f"{pm.num_preds_errored} errored",
        )
    )
    blocks.append(
        kv(
            "transitions:",
            f"{pm.num_trans_total} total, "
            f"{pm.num_trans_matched} matched, "
            f"{pm.num_trans_valid_logic} logic-valid, "
            f"{pm.num_trans_errored} errored",
        )
    )
    blocks.append(Text(""))

    # Spec / scenarios
    blocks.append(Text("Spec / scenarios", style="bold"))
    blocks.append(
        kv(
            "scenarios:",
            f"{pm.num_sc_total} total, "
            f"{pm.num_sc_missing} missing, "
            f"{pm.num_sc_matched} matched, "
            f"{pm.num_sc_inconsistent} inconsistent",
        )
    )
    blocks.append(
        kv(
            "conflicts:",
            f"{pm.num_sc_conflicted} total, "
            f"{pm.num_sc_overlap} overlap, "
            f"{pm.num_sc_consumed} consumed",
        )
    )
    blocks.append(Text(""))

    # Artifacts
    blocks.append(Text("Artifacts", style="bold"))
    blocks.append(
        kv(
            "test traces:",
            f"{pm.num_test_traces_total} total, "
            f"{pm.num_test_traces_logic_good} logic-good, "
            f"{pm.num_test_traces_matched} matched",
        )
    )
    blocks.append(
        kv(
            "log traces:",
            f"{pm.num_log_traces_total} total, "
            f"{pm.num_log_traces_logic_good} logic-good, "
            f"{pm.num_log_traces_matched} matched",
        )
    )

    # NEW: src + docs (optional)
    # Only show if ctx allows and the fields exist on the PM (they should, after your model update).
    if getattr(ctx, "show_artifacts", True):
        if (pm.num_src_code_arts_total is not None) or (pm.num_src_code_arts_matched is not None):
            blocks.append(
                kv(
                    "src code:",
                    f"{pm.num_src_code_arts_total} total, "
                    f"{pm.num_src_code_arts_matched} matched",
                )
            )
        if (pm.num_doc_arts_total is not None) or (pm.num_doc_arts_matched is not None):
            blocks.append(
                kv(
                    "docs:",
                    f"{pm.num_doc_arts_total} total, "
                    f"{pm.num_doc_arts_matched} matched",
                )
            )

    body = Group(*blocks)

    return Panel(
        body,
        title="[bold]Summary[/bold]",
        border_style="magenta",
    )