from typing import Any

from .report_element import CustomReportElement
from .report_layout_response import ReportLayoutResponse


class CustomRenderReportResponse(ReportLayoutResponse):
    """Report response that supports renderers, preparers and named resources."""

    def __init__(self):
        """Creates an empty CustomRenderReportResponse."""
        super().__init__()
        self.resources = {}
        self.renderers = []
        self.preparers = []

    def add_resource(self, name: str, value: Any):
        """Registers a named resource available to renderers and preparers.

        Args:
            name: Resource alias.
            value: Resource data.
        """
        self.resources[name] = value

    def add_renderer(self, renderer: CustomReportElement):
        """Appends a renderer to the response.

        Args:
            renderer: Renderer element to include.
        """
        self.renderers.append(renderer.build_content())

    def add_preparer(self, preparer: CustomReportElement):
        """Appends a preparer to the response.

        Args:
            preparer: Preparer element to include.
        """
        self.preparers.append(preparer.build_content())

    def get_report_data(self):
        """Builds and returns the full report response dict including renderers and preparers.

        Returns:
            Dict extending the base layout response with ``resources``, ``renderers``
            and ``preparers`` sections.
        """
        result = super().get_report_data() or {}

        if len(self.resources or {}) > 0:
            result["resources"] = self.resources
        if len(self.renderers or []) > 0:
            result["renderers"] = self.renderers
        if len(self.preparers or []) > 0:
            result["preparers"] = self.preparers

        return result
