#
#   Imandra Inc.
#
#   speclogician/presentation/preview/fixtures/instances_list.py
#

from __future__ import annotations

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.instances_list import (
    InstancesListPM,
    InstancesListRowPM,
)
from speclogician.presentation.models.state_instance_summary import StateInstanceSummaryPM
from speclogician.utils import IMLValidity
from speclogician.modeling.domain import BaseStatus


# -----------------------------------------------------------------------------
# helpers
# -----------------------------------------------------------------------------

def _summary(
    *,
    created_at: str,
    num_changes: int,
    base_status: BaseStatus = BaseStatus.UNKNOWN,
    base_has_state: bool = False,
    base_has_action: bool = False,
    num_preds_total: int = 0,
    num_preds_matched: int = 0,
    num_trans_total: int = 0,
    num_trans_valid_logic: int = 0,
    num_sc_total: int = 0,
    num_sc_missing: int = 0,
    num_sc_inconsistent: int = 0,
    num_sc_conflicted: int = 0,
    num_test_traces_total: int = 0,
    num_test_traces_logic_good: int = 0,
    num_log_traces_total: int = 0,
    num_log_traces_logic_good: int = 0,
) -> StateInstanceSummaryPM:
    return StateInstanceSummaryPM(
        created_at=created_at,
        num_changes=num_changes,
        base_status=base_status,
        base_has_state=base_has_state,
        base_has_action=base_has_action,
        num_preds_total=num_preds_total,
        num_preds_matched=num_preds_matched,
        num_trans_total=num_trans_total,
        num_trans_valid_logic=num_trans_valid_logic,
        num_sc_total=num_sc_total,
        num_sc_missing=num_sc_missing,
        num_sc_inconsistent=num_sc_inconsistent,
        num_sc_conflicted=num_sc_conflicted,
        num_test_traces_total=num_test_traces_total,
        num_test_traces_logic_good=num_test_traces_logic_good,
        num_log_traces_total=num_log_traces_total,
        num_log_traces_logic_good=num_log_traces_logic_good,
    )


# -----------------------------------------------------------------------------
# fixtures
# -----------------------------------------------------------------------------

def minimal_instances_list_pm() -> InstancesListPM:
    return InstancesListPM(rows=[])


def typical_instances_list_pm() -> InstancesListPM:
    return InstancesListPM(
        rows=[
            InstancesListRowPM(
                state_label="latest",
                changes="DomainModelBaseEdit, PredicateAdd",
                summary=_summary(
                    created_at="2026-01-09T10:12:00Z",
                    num_changes=2,
                    base_status=BaseStatus.VALID,
                    base_has_state=True,
                    base_has_action=True,
                    num_preds_total=12,
                    num_preds_matched=7,
                    num_trans_total=4,
                    num_trans_valid_logic=3,
                    num_sc_total=9,
                    num_sc_missing=1,
                    num_test_traces_total=8,
                    num_test_traces_logic_good=6,
                    num_log_traces_total=4,
                    num_log_traces_logic_good=4,
                ),
            ),
            InstancesListRowPM(
                state_label="1",
                changes="PredicateEdit",
                summary=_summary(
                    created_at="2026-01-09T09:55:10Z",
                    num_changes=1,
                    base_status=BaseStatus.VALID,
                    base_has_state=True,
                    base_has_action=True,
                    num_preds_total=11,
                    num_preds_matched=6,
                    num_trans_total=4,
                    num_trans_valid_logic=3,
                    num_sc_total=8,
                    num_sc_missing=0,
                    num_test_traces_total=7,
                    num_test_traces_logic_good=6,
                    num_log_traces_total=3,
                    num_log_traces_logic_good=3,
                ),
            ),
        ]
    )


def edge_instances_list_pm() -> InstancesListPM:
    return InstancesListPM(
        rows=[
            InstancesListRowPM(
                state_label="latest",
                changes="",
                summary=_summary(
                    created_at="2026-01-09T10:12:00.123456Z",
                    num_changes=0,
                    base_status=BaseStatus.UNKNOWN,
                    base_has_state=False,
                    base_has_action=False,
                    num_preds_total=0,
                    num_preds_matched=0,
                    num_trans_total=0,
                    num_trans_valid_logic=0,
                    num_sc_total=0,
                    num_sc_missing=0,
                    num_test_traces_total=0,
                    num_test_traces_logic_good=0,
                    num_log_traces_total=0,
                    num_log_traces_logic_good=0,
                ),
            )
        ]
    )

def make_instances_list_pm(ctx: RenderCtx, variant: str = "typical") -> InstancesListPM:
    v = (variant or "typical").strip().lower()

    if v == "minimal":
        return minimal_instances_list_pm()

    if v == "edge":
        return edge_instances_list_pm()

    return typical_instances_list_pm()


def render(pm: InstancesListPM, ctx : RenderCtx):
    from speclogician.presentation.renderers.instances_list import render_instances_list
    return render_instances_list(pm, ctx=ctx)