from __future__ import annotations

from typing import Any

from app.agents.orchestration.monitor import build_parallel_runtime_metrics


def build_runtime_observability_report(
    *,
    state: dict[str, Any],
    feature_flags: dict[str, bool] | None = None,
) -> dict[str, Any]:
    task_graph = state.get("task_graph") if isinstance(state.get("task_graph"), dict) else None
    scheduled_subtasks = (
        state.get("scheduled_subtasks") if isinstance(state.get("scheduled_subtasks"), list) else []
    )
    task_results = state.get("task_results") if isinstance(state.get("task_results"), list) else []
    merge_report = state.get("merge_report") if isinstance(state.get("merge_report"), dict) else None

    return {
        "execution_mode": state.get("execution_mode"),
        "verification_status": state.get("verification_status"),
        "skill_shortlist_count": len(state.get("skill_shortlist") or []),
        "retrospective_shortlist_count": len(state.get("retrospective_shortlist") or []),
        "feature_flags": feature_flags or {},
        "parallel_metrics": build_parallel_runtime_metrics(
            task_graph=task_graph,
            scheduled_subtasks=scheduled_subtasks,
            task_results=task_results,
            merge_report=merge_report,
        ),
    }
