from __future__ import annotations

from typing import Any


def build_parallel_runtime_metrics(
    *,
    task_graph: dict[str, Any] | None,
    scheduled_subtasks: list[dict[str, Any]] | None,
    task_results: list[dict[str, Any]] | None,
    merge_report: dict[str, Any] | None,
) -> dict[str, Any]:
    task_graph = task_graph or {}
    scheduled_subtasks = list(scheduled_subtasks or [])
    task_results = list(task_results or [])
    merge_report = merge_report or {}

    completed = sum(1 for item in task_results if item.get("status") == "completed")
    failed = sum(1 for item in task_results if item.get("status") == "failed")
    blocked = sum(1 for item in task_results if item.get("status") == "blocked")

    return {
        "task_graph_node_count": len(task_graph.get("nodes") or []),
        "scheduled_subtask_count": len(scheduled_subtasks),
        "completed_subtask_count": completed,
        "failed_subtask_count": failed,
        "blocked_subtask_count": blocked,
        "merge_status": merge_report.get("status"),
        "merge_conflict_count": len(merge_report.get("conflict_flags") or []),
        "fallback_used": bool(merge_report.get("fallback_used") or False),
    }
