#
#   Imandra Inc.
#
#   speclogician/logic/api/traces.py
#

from __future__ import annotations

import asyncio

from .client import SpecLogicianImandraX
from speclogician.modeling.domain import DomainModel
from speclogician.modeling.scenario import Scenario
from speclogician.data.traces import TraceArtifact, TraceIMLValidity, TraceScenarioMatchResult


async def check_trace_iml_validity_async(
    tr: TraceArtifact,
    model: DomainModel,
) -> TraceIMLValidity:
    """
    Check that trace snippets typecheck against the domain model.
    """
    async with SpecLogicianImandraX() as ix:
        v = await ix.check_trace_iml_validity(tr=tr, model=model)
        tr.iml_validity = v
        return v

def check_trace_iml_validity (
    tr: TraceArtifact,
    model: DomainModel,
) -> TraceIMLValidity:
    """
    Sync wrapper around check_trace_iml_validity_async.
    """
    return asyncio.run(check_trace_iml_validity_async(tr, model))


async def check_trace_match_async(
    tr: TraceArtifact,
    s: Scenario,
    model: DomainModel,
) -> TraceScenarioMatchResult:
    """
    Check whether the trace matches a scenario (given/when predicates + transition result).
    Assumes check_trace_iml_validity is either called inside or is handled by the async method.
    """
    async with SpecLogicianImandraX() as ix:
        return await ix.check_trace_match(tr=tr, s=s, model=model)

def check_trace_match(
    tr: TraceArtifact,
    s: Scenario,
    model: DomainModel,
) -> TraceScenarioMatchResult:
    """
    Sync wrapper around check_trace_match_async.
    """
    return asyncio.run(check_trace_match_async(tr, s, model))