#
#   Imandra Inc.
#
#   speclogician/presentation/builders/state_instance_summary.py
#

from __future__ import annotations

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.state_instance_summary import StateInstanceSummaryPM
from speclogician.state.inst import StateInstance


def _g(obj: object, name: str, default):
    return getattr(obj, name, default)


def build_state_instance_summary_pm(si: StateInstance, ctx: RenderCtx) -> StateInstanceSummaryPM:
    dm = si.spec.domain_model
    spec = si.spec
    art = si.art_container

    return StateInstanceSummaryPM(
        created_at=si.created_at.isoformat() if si.created_at else None,
        num_changes=len(si.changes or []),

        # ---- Domain model (base) ----
        base_status=str(_g(dm, "base_status", None)),
        base_has_state=_g(dm, "base_has_state", False),
        base_has_action=_g(dm, "base_has_action", False),

        # ---- State predicates ----
        num_state_preds_total=_g(dm, "num_state_preds_total", 0),
        num_state_preds_matched=_g(dm, "num_state_preds_matched", 0),
        num_state_preds_valid_logic=_g(dm, "num_state_preds_valid_logic", 0),
        num_state_preds_errored=_g(dm, "num_state_preds_errored", 0),

        # ---- Action predicates ----
        num_action_preds_total=_g(dm, "num_action_preds_total", 0),
        num_action_preds_matched=_g(dm, "num_action_preds_matched", 0),
        num_action_preds_valid_logic=_g(dm, "num_action_preds_valid_logic", 0),
        num_action_preds_errored=_g(dm, "num_action_preds_errored", 0),

        # ---- Predicates totals ----
        num_preds_total=_g(dm, "num_preds_total", 0),
        num_preds_matched=_g(dm, "num_preds_matched", 0),
        num_preds_valid_logic=_g(dm, "num_preds_valid_logic", 0),
        num_preds_errored=_g(dm, "num_preds_errored", 0),

        # ---- Transitions ----
        num_trans_total=_g(dm, "num_trans_total", 0),
        num_trans_matched=_g(dm, "num_trans_matched", 0),
        num_trans_valid_logic=_g(dm, "num_trans_valid_logic", 0),
        num_trans_errored=_g(dm, "num_trans_errored", 0),

        # ---- Scenarios ----
        num_sc_total=_g(spec, "num_sc_total", 0),
        num_sc_missing=_g(spec, "num_sc_missing", 0),
        num_sc_matched=_g(spec, "num_sc_matched", 0),
        num_sc_inconsistent=_g(spec, "num_sc_inconsistent", 0),

        # ---- Conflicts (spec-level) ----
        num_sc_conflicted=_g(spec, "num_sc_conflicted", 0),
        num_sc_overlap=_g(spec, "num_sc_overlap", 0),
        num_sc_consumed=_g(spec, "num_sc_consumed", 0),

        # ---- ArtifactContainer: test traces ----
        num_test_traces_total=_g(art, "num_test_traces_total", 0),
        num_test_traces_logic_good=_g(art, "num_test_traces_logic_good", 0),
        num_test_traces_matched=_g(art, "num_test_traces_matched", 0),

        # ---- ArtifactContainer: log traces ----
        num_log_traces_total=_g(art, "num_log_traces_total", 0),
        num_log_traces_logic_good=_g(art, "num_log_traces_logic_good", 0),
        num_log_traces_matched=_g(art, "num_log_traces_matched", 0),

        # ---- ArtifactContainer: src code ----
        num_src_code_arts_total=_g(art, "num_src_code_arts_total", 0),
        num_src_code_arts_matched=_g(art, "num_src_code_arts_matched", 0),

        # ---- ArtifactContainer: docs ----
        num_doc_arts_total=_g(art, "num_doc_arts_total", 0),
        num_doc_arts_matched=_g(art, "num_doc_arts_matched", 0),
    )