from typing import Any

from messageprocessorutil import DataSerializer

from boh.report.layout import ReportLayoutResponse


class ReportLayoutSerializer(DataSerializer):
    """Serializer for ReportLayoutResponse — extracts data via get_report_data()."""

    def __init__(self, report_name: str) -> None:
        """Args:
        report_name: Used in error messages.
        """
        self._report_name = report_name

    def serialize(self, result: ReportLayoutResponse) -> dict:
        """Serializes a ReportLayoutResponse into a plain dict.

        Args:
            result: Report response to serialize.

        Returns:
            Dict produced by ``result.get_report_data()``.
        """
        return result.get_report_data()

    def serialize_error(self, input_obj: Any, exception: Exception, original_payload: dict) -> dict:
        """Builds a business-error response.

        Args:
            input_obj: Parsed request object the business step received.
            exception: Exception raised during the business step.
            original_payload: Original decoded payload.

        Returns:
            Dict with report-id, error flag, and error message.
        """
        return {
            "report-id": original_payload.get("report-id"),
            "report-error-message": f"Failed to process {self._report_name} report data: {exception}",
            "error": True,
        }

    def serialize_parser_error(self, data: bytes, exception: Exception) -> dict:
        """Builds a parse-error response.

        Args:
            data: Raw bytes that failed to parse.
            exception: Exception raised during parsing.

        Returns:
            Dict with error flag and error message (no report-id since parsing may not have been assigned).
        """
        return {
            "report-error-message": f"Failed to parse {self._report_name} request: {exception}",
            "error": True,
        }
