"""CustomRenderReportResponse — composition of resources, renderers and preparers
into a structured report payload via get_report_data().
"""

from boh.report.layout import CustomReportNamedResource, ReportTitle
from boh.report.layout import CustomRenderReportResponse
from boh.report.layout.preparer import DisableXlsLayoutFieldsPreparer, ConfigureSheetTablesPreparer
from boh.report.layout.renderer import TableRenderer, SheetTableRenderer


# --- resources ------------------------------------------------------------


def test_resources_appear_in_report_data_when_added():
    response = CustomRenderReportResponse()
    response.add_resource("columns", [{"id": "name", "header": "$NAME"}])

    result = response.get_report_data()

    assert result["resources"]["columns"] == [{"id": "name", "header": "$NAME"}]


def test_resources_are_absent_from_report_data_when_none_added():
    response = CustomRenderReportResponse()

    result = response.get_report_data()

    assert "resources" not in result


# --- renderers ------------------------------------------------------------


def test_renderer_appears_in_report_data_when_added():
    response = CustomRenderReportResponse()
    response.add_resource("cols", [])
    response.add_resource("data", [])
    response.add_renderer(
        TableRenderer(
            columns=CustomReportNamedResource(name="cols", type="resources"),
            data=CustomReportNamedResource(name="data", type="resources"),
        )
    )

    result = response.get_report_data()

    assert len(result["renderers"]) == 1
    assert result["renderers"][0]["name"] == "table"


def test_multiple_renderers_are_ordered_by_insertion(message_bus=None):
    response = CustomRenderReportResponse()
    response.add_renderer(
        TableRenderer(
            columns=CustomReportNamedResource("cols", "resources"),
            data=CustomReportNamedResource("data", "resources"),
        )
    )
    response.add_renderer(
        SheetTableRenderer(
            columns=CustomReportNamedResource("cols", "resources"),
            data=CustomReportNamedResource("data", "resources"),
        )
    )

    result = response.get_report_data()

    assert result["renderers"][0]["name"] == "table"
    assert result["renderers"][1]["name"] == "sheet_table"


def test_renderers_are_absent_from_report_data_when_none_added():
    response = CustomRenderReportResponse()

    result = response.get_report_data()

    assert "renderers" not in result


# --- preparers ------------------------------------------------------------


def test_preparer_appears_in_report_data_when_added():
    response = CustomRenderReportResponse()
    response.add_preparer(DisableXlsLayoutFieldsPreparer())

    result = response.get_report_data()

    assert len(result["preparers"]) == 1
    assert result["preparers"][0]["name"] == "disable_xls_layout_fields"


def test_multiple_preparers_are_ordered_by_insertion():
    response = CustomRenderReportResponse()
    response.add_preparer(DisableXlsLayoutFieldsPreparer())
    response.add_preparer(ConfigureSheetTablesPreparer())

    result = response.get_report_data()

    assert result["preparers"][0]["name"] == "disable_xls_layout_fields"
    assert result["preparers"][1]["name"] == "configure_sheet_tables"


def test_preparers_are_absent_from_report_data_when_none_added():
    response = CustomRenderReportResponse()

    result = response.get_report_data()

    assert "preparers" not in result


# --- filters and base sections --------------------------------------------


def test_filter_appears_in_report_data_when_added():
    response = CustomRenderReportResponse()
    response.add_filter(ReportTitle("$DATE_RANGE", ["2026-01-01", "2026-01-31"]))

    result = response.get_report_data()

    assert result["filters"]["fields"][0]["title"] == {
        "text": "$DATE_RANGE",
        "params": ["2026-01-01", "2026-01-31"],
    }


def test_empty_flag_is_included_when_set():
    response = CustomRenderReportResponse()
    response.set_empty(True)

    result = response.get_report_data()

    assert result["empty"] is True


def test_empty_flag_is_absent_when_not_set():
    response = CustomRenderReportResponse()

    result = response.get_report_data()

    assert "empty" not in result
