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

from __future__ import annotations

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

from speclogician.presentation.builders.trace import build_test_trace_pm, build_log_trace_pm
from speclogician.presentation.builders.data_artifact import build_doc_ref_pm, build_src_code_ref_pm


def build_smart_find_pm(res: SmartFindResults, ctx: RenderCtx) -> SmartFindPM:
    counts = SmartFindCountsPM(
        needle=res.needle,
        num_test_traces=len(res.test_traces),
        num_log_traces=len(res.log_traces),
        num_doc_refs=len(res.doc_refs),
        num_src_code_refs=len(res.src_code_refs),
    )

    items = SmartFindItemsPM()

    # If stats-only, skip the heavy lists.
    if not ctx.show_stats_only:
        items.test_traces = [build_test_trace_pm(t, ctx) for t in res.test_traces]
        items.log_traces = [build_log_trace_pm(t, ctx) for t in res.log_traces]
        items.doc_refs = [build_doc_ref_pm(d, ctx) for d in res.doc_refs]
        items.src_code_refs = [build_src_code_ref_pm(s, ctx) for s in res.src_code_refs]

    return SmartFindPM(counts=counts, items=items)
