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

from __future__ import annotations
from speclogician.presentation.ctx import RenderCtx

from speclogician.presentation.models.container import (
    ArtifactContainerPM,
    ArtifactContainerCountsPM,
    ArtifactContainerItemsPM,
)

# reuse existing fixtures
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,
)


# -----------------------------------------------------------------------------
# helpers
# -----------------------------------------------------------------------------

def _counts_from_items(items: ArtifactContainerItemsPM) -> ArtifactContainerCountsPM:
    # totals
    num_test_total = len(items.test_traces)
    num_log_total = len(items.log_traces)
    num_doc_total = len(items.doc_refs)
    num_src_total = len(items.src_code_refs)

    # matched / logic_good are not explicitly present on PMs yet,
    # so we keep these deterministic for fixtures. You can refine
    # this later once you encode matched flags in PMs.
    def _half(n: int) -> int:
        return 0 if n == 0 else max(1, n // 2)

    return ArtifactContainerCountsPM(
        num_test_traces_total=num_test_total,
        num_test_traces_matched=_half(num_test_total),
        num_test_traces_logic_good=_half(num_test_total),

        num_log_traces_total=num_log_total,
        num_log_traces_matched=_half(num_log_total),
        num_log_traces_logic_good=_half(num_log_total),

        num_src_code_arts_total=num_src_total,
        num_src_code_arts_matched=_half(num_src_total),

        num_doc_arts_total=num_doc_total,
        num_doc_arts_matched=_half(num_doc_total),
    )


# -----------------------------------------------------------------------------
# main fixture entrypoint (matches PreviewSpec.make signature)
# -----------------------------------------------------------------------------

def make_artifact_container_pm(ctx: RenderCtx, variant: str = "typical") -> ArtifactContainerPM:
    v = (variant or "typical").lower().strip()

    # -------------------------
    # minimal
    # -------------------------
    if v == "minimal":
        items = ArtifactContainerItemsPM(
            test_traces=[],
            log_traces=[],
            doc_refs=[],
            src_code_refs=[],
        )
        return ArtifactContainerPM(
            counts=_counts_from_items(items),
            items=items,
        )

    # -------------------------
    # edge (stress: long content, many items)
    # -------------------------
    if v == "edge":
        items = ArtifactContainerItemsPM(
            test_traces=[
                edge_test_trace_pm(),
                typical_test_trace_pm(with_contents=True, invalid=True),
                typical_test_trace_pm(with_contents=False, invalid=False),
            ],
            log_traces=[
                edge_log_trace_pm(),
                typical_log_trace_pm(with_contents=True, invalid=True),
            ],
            doc_refs=[
                edge_doc_ref_pm(),
                typical_doc_ref_pm(),
            ],
            src_code_refs=[
                edge_src_code_ref_pm(),
                typical_src_code_ref_pm(),
            ],
        )
        return ArtifactContainerPM(
            counts=_counts_from_items(items),
            items=items,
        )

    # -------------------------
    # typical
    # -------------------------
    items = ArtifactContainerItemsPM(
        test_traces=[
            typical_test_trace_pm(with_contents=False, invalid=False),
            typical_test_trace_pm(with_contents=True, invalid=True),
        ],
        log_traces=[
            typical_log_trace_pm(with_contents=False, invalid=False),
        ],
        doc_refs=[
            typical_doc_ref_pm(),
        ],
        src_code_refs=[
            typical_src_code_ref_pm(),
        ],
    )
    return ArtifactContainerPM(
        counts=_counts_from_items(items),
        items=items,
    )


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

def render(pm: ArtifactContainerPM, ctx : RenderCtx):
    from speclogician.presentation.renderers.container import render_artifact_container
    return render_artifact_container(pm, ctx=ctx)