"""Human-first implementation delivery view model."""
from __future__ import annotations

from ..common import evidence_index
from ..models import HumanReportView, VisualNode
from ..visualizations import change_map_figure


_FILE_ACTIONS = {"created": "Created", "modified": "Modified", "deleted": "Deleted"}


def _change_nodes(implementation: dict) -> tuple[VisualNode, ...]:
    return tuple(
        VisualNode(
            row["file"],
            row["file"],
            row["planStep"],
            row["action"],
            row["lines"],
            note=f'{_FILE_ACTIONS.get(row["action"], row["action"])} · plan step {row["planStep"]}',
        )
        for row in implementation["diffSummary"]["files"]
    )


def build_implementation_view(data: dict) -> HumanReportView:
    implementation = data["implementation"]
    figure = change_map_figure(
        nodes=_change_nodes(implementation), title="Delivered change areas"
    )
    context = {
        "humanSummary": data["humanSummary"],
        "implementation": implementation,
        "narrative": implementation["userNarrative"],
        "changeFigure": figure,
        "evidenceIndex": evidence_index(data),
    }
    return HumanReportView(
        "implementation",
        "html/tasks/implementation.template.html",
        context,
        (figure,),
    )
