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

from __future__ import annotations

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.smart_find import (
    SmartFindPM,
    SmartFindCountsPM,
    SmartFindItemsPM,
)

from speclogician.presentation.preview.fixtures.trace import (
    minimal_test_trace_pm,
    typical_test_trace_pm,
    edge_test_trace_pm,
    minimal_log_trace_pm,
    typical_log_trace_pm,
    edge_log_trace_pm,
)

from speclogician.presentation.preview.fixtures.data_artifact import (
    minimal_doc_ref_pm,
    typical_doc_ref_pm,
    edge_doc_ref_pm,
    minimal_src_code_ref_pm,
    typical_src_code_ref_pm,
    edge_src_code_ref_pm,
)


# -----------------------------------------------------------------------------
# variants
# -----------------------------------------------------------------------------

def _smart_find_minimal() -> SmartFindPM:
    needle = ""
    return SmartFindPM(
        counts=SmartFindCountsPM(
            needle=needle,
            num_test_traces=0,
            num_log_traces=0,
            num_doc_refs=0,
            num_src_code_refs=0,
        ),
        items=SmartFindItemsPM(
            test_traces=[],
            log_traces=[],
            doc_refs=[],
            src_code_refs=[],
        ),
    )


def _smart_find_typical() -> SmartFindPM:
    needle = "order size"
    tt = typical_test_trace_pm(with_contents=False, invalid=False)
    lt = typical_log_trace_pm(with_contents=False, invalid=False)
    dr = typical_doc_ref_pm()
    sr = typical_src_code_ref_pm()

    return SmartFindPM(
        counts=SmartFindCountsPM(
            needle=needle,
            num_test_traces=1,
            num_log_traces=1,
            num_doc_refs=1,
            num_src_code_refs=1,
        ),
        items=SmartFindItemsPM(
            test_traces=[tt],
            log_traces=[lt],
            doc_refs=[dr],
            src_code_refs=[sr],
        ),
    )


def _smart_find_edge() -> SmartFindPM:
    needle = "pred_eq_ten OR max_qty (line:42)"
    # Stress renderers: mix long contents + invalid + lots of items
    tts = [
        typical_test_trace_pm(with_contents=True, invalid=True),
        edge_test_trace_pm(),
    ]
    lts = [
        typical_log_trace_pm(with_contents=True, invalid=True),
        edge_log_trace_pm(),
    ]
    drs = [
        edge_doc_ref_pm(),
        typical_doc_ref_pm(),
        minimal_doc_ref_pm(),
    ]
    srs = [
        edge_src_code_ref_pm(),
        typical_src_code_ref_pm(),
        minimal_src_code_ref_pm(),
    ]

    return SmartFindPM(
        counts=SmartFindCountsPM(
            needle=needle,
            num_test_traces=len(tts),
            num_log_traces=len(lts),
            num_doc_refs=len(drs),
            num_src_code_refs=len(srs),
        ),
        items=SmartFindItemsPM(
            test_traces=tts,
            log_traces=lts,
            doc_refs=drs,
            src_code_refs=srs,
        ),
    )


# -----------------------------------------------------------------------------
# top-level fixture entrypoint (matches your preview setup)
# -----------------------------------------------------------------------------

def make_smart_find_pm(ctx: RenderCtx, variant: str = "typical") -> SmartFindPM:
    v = (variant or "typical").strip().lower()
    if v == "minimal":
        return _smart_find_minimal()
    if v == "edge":
        return _smart_find_edge()
    return _smart_find_typical()


# -----------------------------------------------------------------------------
# renderer adapter (matches other fixtures style)
# -----------------------------------------------------------------------------

def render(pm: SmartFindPM, ctx: RenderCtx):
    from speclogician.presentation.renderers.smart_find import render_smart_find
    return render_smart_find(pm, ctx=ctx)