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

from __future__ import annotations

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.models.recommendations import (
    RecommendationsPM,
    RecommendationPM,
)
from speclogician.state.recommender import Recommendation


def build_recommendations_pm(
    recs: list[Recommendation] | None,
    ctx: RenderCtx,
) -> RecommendationsPM:
    rs = list(recs or [])

    # UI wants stable ordering: priority asc, then kind, then text
    rs.sort(key=lambda r: (r.priority, r.kind, r.text))

    items = [RecommendationPM(kind=r.kind, priority=r.priority, text=r.text) for r in rs]

    # counts
    n_error = sum(1 for r in items if r.kind == "error")
    n_warn = sum(1 for r in items if r.kind == "warning")
    n_next = sum(1 for r in items if r.kind == "next")
    n_info = sum(1 for r in items if r.kind == "info")

    return RecommendationsPM(
        count=len(items),
        items=items,
        num_error=n_error,
        num_warning=n_warn,
        num_next=n_next,
        num_info=n_info,
    )