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

from __future__ import annotations

from speclogician.modeling.predicates import ActionPredicate, PredicateType, StatePredicate, Transition
from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.builders._links import build_links_pm
from speclogician.presentation.models.predicate import PredicatePM, TransitionPM


def build_predicate_pm(p: StatePredicate | ActionPredicate, ctx: RenderCtx) -> PredicatePM:
    pred_type = PredicateType.STATE if isinstance(p, StatePredicate) else PredicateType.ACTION
    signature = f"{p.name} s" if pred_type == PredicateType.STATE else f"{p.name} s a"

    # Prefer to_iml if present (yours is)
    iml = p.to_iml() if hasattr(p, "to_iml") else ""

    return PredicatePM(
        kind=p.kind,  # "state_predicate" | "action_predicate"
        comp_id=p.comp_id,
        name=p.name,
        last_updated=p.last_updated,
        pred_type=pred_type,
        signature=signature,
        src_code=p.src_code or "",
        iml=iml,
        is_iml_valid=p.is_iml_valid,
        iml_error=p.iml_error,
        links=build_links_pm(p.comp_id, ctx),
    )


def build_transition_pm(t: Transition, ctx: RenderCtx) -> TransitionPM:
    signature = f"{t.name} s a"
    iml = t.to_iml() if hasattr(t, "to_iml") else ""

    return TransitionPM(
        comp_id=t.comp_id,
        name=t.name,
        last_updated=t.last_updated,
        signature=signature,
        src_code=t.src_code or "",
        iml=iml,
        is_iml_valid=t.is_iml_valid,
        iml_error=t.iml_error,
        links=build_links_pm(t.comp_id, ctx),
    )