"""Renderers — build_content() output for TableRenderer, SheetTableRenderer
and GroupTableRenderer, including conditions and optional params.
"""

from boh.report.layout import CustomReportConditions, CustomReportNamedResource, ReportTitle
from boh.report.layout.renderer import GroupTableRenderer, SheetTableRenderer, TableRenderer


# --- TableRenderer --------------------------------------------------------


def test_table_renderer_builds_name_and_required_params():
    renderer = TableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
    )
    content = renderer.build_content()

    assert content["name"] == "table"
    assert content["params"]["columns"] == {"name": "cols", "type": "resources"}
    assert content["params"]["data"] == {"name": "rows", "type": "resources"}


def test_table_renderer_includes_optional_params_when_set():
    renderer = TableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
        style="compact",
        auto_filter=True,
        auto_fit_width=True,
        sheet_protection=False,
    )
    params = renderer.build_content()["params"]

    assert params["style"] == "compact"
    assert params["auto_filter"] is True
    assert params["auto_fit_width"] is True
    assert params["sheet_protection"] is False


def test_table_renderer_omits_none_optional_params():
    renderer = TableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
    )
    params = renderer.build_content()["params"]

    assert "style" not in params
    assert "auto_filter" not in params


def test_table_renderer_serializes_title_as_title_content():
    renderer = TableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
        title=ReportTitle("$REPORT_TITLE"),
    )
    assert renderer.build_content()["params"]["title"] == "$REPORT_TITLE"


def test_table_renderer_includes_conditions_when_set():
    renderer = TableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
        conditions=[CustomReportConditions.IS_PDF],
    )
    assert renderer.build_content()["conditions"] == ["is_pdf"]


# --- SheetTableRenderer ---------------------------------------------------


def test_sheet_table_renderer_builds_name_and_required_params():
    renderer = SheetTableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
    )
    content = renderer.build_content()

    assert content["name"] == "sheet_table"
    assert content["params"]["columns"] == {"name": "cols", "type": "resources"}


def test_sheet_table_renderer_includes_sheet_name_when_set():
    renderer = SheetTableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
        sheet_name="Store A",
        sheet_index_offset=1,
        limit=500,
    )
    params = renderer.build_content()["params"]

    assert params["sheet_name"] == "Store A"
    assert params["sheet_index_offset"] == 1
    assert params["limit"] == 500


def test_sheet_table_renderer_includes_xls_condition():
    renderer = SheetTableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        data=CustomReportNamedResource("rows", "resources"),
        conditions=[CustomReportConditions.IS_XLS, CustomReportConditions.CONFIGURE_SHEET_TABLES],
    )
    assert renderer.build_content()["conditions"] == ["is_xls", "configure_sheet_tables"]


# --- GroupTableRenderer ---------------------------------------------------


def test_group_table_renderer_builds_name_and_required_params():
    renderer = GroupTableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        group_name="grouped_by_shift",
    )
    content = renderer.build_content()

    assert content["name"] == "group_tables"
    assert content["params"]["group_name"] == "grouped_by_shift"
    assert content["params"]["columns"] == {"name": "cols", "type": "resources"}


def test_group_table_renderer_includes_optional_params_when_set():
    renderer = GroupTableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        group_name="by_shift",
        style="bold",
        group_title="shift_label",
        group_spacing=5,
    )
    params = renderer.build_content()["params"]

    assert params["style"] == "bold"
    assert params["group_title"] == "shift_label"
    assert params["group_spacing"] == 5


def test_group_table_renderer_includes_pdf_condition():
    renderer = GroupTableRenderer(
        columns=CustomReportNamedResource("cols", "resources"),
        group_name="by_shift",
        conditions=[CustomReportConditions.IS_PDF],
    )
    assert renderer.build_content()["conditions"] == ["is_pdf"]
