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

from __future__ import annotations

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.state_instance import StateInstancePM
from speclogician.presentation.models.recommendations import (
    RecommendationPM,
    RecommendationsPM,
)

from .state_instance_summary import make_state_instance_summary_pm
from .spec import make_spec_pm
from .artifact_container import make_artifact_container_pm
from .state_diff import make_state_diff_pm


# -----------------------------------------------------------------------------
# Shared recommendations PMs
# -----------------------------------------------------------------------------

_EMPTY_RECOMMENDATIONS = RecommendationsPM(
    count=0,
    items=[],
    num_error=0,
    num_warning=0,
    num_next=0,
    num_info=0,
)

_TYPICAL_RECOMMENDATIONS = RecommendationsPM(
    count=4,
    items=[
        RecommendationPM(
            kind="error",
            priority=0,
            text=(
                "Domain model base declined (VALID → INVALID_IML). "
                "Fix base IML/type issues before trusting any further analysis."
            ),
        ),
        RecommendationPM(
            kind="warning",
            priority=8,
            text=(
                "Some scenarios reference predicates/transitions that do not exist yet.\n"
                "- `HappyPath` missing preds=['p_user_exists'] trans=['t_create_user']\n"
                "Recommendation: add the missing predicates/transitions or edit the scenarios to match the domain model."
            ),
        ),
        RecommendationPM(
            kind="next",
            priority=25,
            text=(
                "More transitions are logic-valid (1 → 3). "
                "Attach these transitions to scenarios’ `then` lists so traces can match end-to-end."
            ),
        ),
        RecommendationPM(
            kind="info",
            priority=40,
            text=(
                "More log traces matched scenarios (0 → 2). "
                "Treat these as in-the-wild confirmation and use them to justify model boundaries."
            ),
        ),
    ],
    num_error=1,
    num_warning=1,
    num_next=1,
    num_info=1,
)


# -----------------------------------------------------------------------------
# Variants
# -----------------------------------------------------------------------------

def _state_instance_minimal(ctx: RenderCtx) -> StateInstancePM:
    return StateInstancePM(
        summary=make_state_instance_summary_pm(ctx, "minimal"),
        recommendations=_EMPTY_RECOMMENDATIONS,
        spec=None,
        artifacts=None,
        state_diff=None,
    )


def _state_instance_typical(ctx: RenderCtx) -> StateInstancePM:
    return StateInstancePM(
        summary=make_state_instance_summary_pm(ctx, "typical"),
        recommendations=_TYPICAL_RECOMMENDATIONS,
        spec=make_spec_pm(ctx, "typical"),
        artifacts=make_artifact_container_pm(ctx, "typical"),
        state_diff=make_state_diff_pm(ctx, "typical"),
    )


def _state_instance_edge(ctx: RenderCtx) -> StateInstancePM:
    return StateInstancePM(
        summary=make_state_instance_summary_pm(ctx, "edge"),
        recommendations=_EMPTY_RECOMMENDATIONS,
        spec=make_spec_pm(ctx, "edge"),
        artifacts=make_artifact_container_pm(ctx, "edge"),
        state_diff=make_state_diff_pm(ctx, "edge"),
    )


# -----------------------------------------------------------------------------
# Public entry point (used by PreviewSpec)
# -----------------------------------------------------------------------------

def make_state_instance_pm(ctx: RenderCtx, variant: str = "typical") -> StateInstancePM:
    v = (variant or "typical").strip().lower()
    if v == "minimal":
        return _state_instance_minimal(ctx)
    if v == "edge":
        return _state_instance_edge(ctx)
    return _state_instance_typical(ctx)


def render(pm: StateInstancePM, ctx: RenderCtx):
    from speclogician.presentation.renderers.state_instance import render_state_instance
    return render_state_instance(pm, ctx=ctx)