"""Preparers — build_content() output for all preparer classes,
including params, conditions and name contract.
"""

from boh.report.layout import CustomReportConditions, CustomReportNamedResource
from boh.report.layout.preparer import (
    AddBoldStylePreparer,
    AddFormatFieldPreparer,
    ConfigureSheetTablesPreparer,
    DisableLayoutFieldsPreparer,
    DisableXlsLayoutFieldsPreparer,
    ExcludeDataPreparer,
    GroupDataPreparer,
)


_RESOURCE = CustomReportNamedResource(name="data", type="resources")


# --- AddBoldStylePreparer -------------------------------------------------


def test_add_bold_style_builds_name_and_required_params():
    preparer = AddBoldStylePreparer(fields=["name", "total"], resource=_RESOURCE)
    content = preparer.build_content()

    assert content["name"] == "add_bold_style"
    assert content["params"]["fields"] == ["name", "total"]
    assert content["params"]["resource"] == {"name": "data", "type": "resources"}


def test_add_bold_style_includes_bold_field_when_set():
    preparer = AddBoldStylePreparer(fields=["total"], resource=_RESOURCE, bold_field="is_total")
    assert preparer.build_content()["params"]["bold_field"] == "is_total"


def test_add_bold_style_includes_conditions_when_set():
    preparer = AddBoldStylePreparer(fields=["total"], resource=_RESOURCE, conditions=[CustomReportConditions.IS_PDF])
    assert preparer.build_content()["conditions"] == ["is_pdf"]


# --- AddFormatFieldPreparer -----------------------------------------------


def test_add_format_field_builds_name_and_all_params():
    preparer = AddFormatFieldPreparer(
        field_name="full_name",
        fields=["first", "last"],
        format="{first} {last}",
        resource=_RESOURCE,
    )
    content = preparer.build_content()

    assert content["name"] == "add_format_field"
    assert content["params"]["field_name"] == "full_name"
    assert content["params"]["fields"] == ["first", "last"]
    assert content["params"]["format"] == "{first} {last}"
    assert content["params"]["resource"] == {"name": "data", "type": "resources"}


# --- ConfigureSheetTablesPreparer -----------------------------------------


def test_configure_sheet_tables_builds_correct_name():
    content = ConfigureSheetTablesPreparer().build_content()

    assert content["name"] == "configure_sheet_tables"
    assert "params" not in content


# --- DisableLayoutFieldsPreparer ------------------------------------------


def test_disable_layout_fields_includes_field_list_as_params():
    preparer = DisableLayoutFieldsPreparer(params=["code", "internal_id"])
    content = preparer.build_content()

    assert content["name"] == "disable_layout_fields"
    assert content["params"] == ["code", "internal_id"]


# --- DisableXlsLayoutFieldsPreparer ---------------------------------------


def test_disable_xls_layout_fields_builds_correct_name():
    content = DisableXlsLayoutFieldsPreparer().build_content()

    assert content["name"] == "disable_xls_layout_fields"
    assert "params" not in content


# --- ExcludeDataPreparer --------------------------------------------------


def test_exclude_data_builds_name_and_required_params():
    preparer = ExcludeDataPreparer(field_name="status", resource=_RESOURCE)
    content = preparer.build_content()

    assert content["name"] == "exclude_data"
    assert content["params"]["field_name"] == "status"
    assert content["params"]["resource"] == {"name": "data", "type": "resources"}


def test_exclude_data_includes_excluded_resource_when_set():
    excluded = CustomReportNamedResource(name="excluded_ids", type="resources")
    preparer = ExcludeDataPreparer(field_name="id", resource=_RESOURCE, excluded_resource=excluded)

    assert preparer.build_content()["params"]["excluded_resource"] == {
        "name": "excluded_ids",
        "type": "resources",
    }


def test_exclude_data_omits_excluded_resource_when_not_set():
    preparer = ExcludeDataPreparer(field_name="id", resource=_RESOURCE)

    assert "excluded_resource" not in preparer.build_content()["params"]


# --- GroupDataPreparer ----------------------------------------------------


def test_group_data_builds_name_and_all_params():
    preparer = GroupDataPreparer(
        group_name="by_shift",
        fields=["shift"],
        resource=_RESOURCE,
    )
    content = preparer.build_content()

    assert content["name"] == "group_data"
    assert content["params"]["group_name"] == "by_shift"
    assert content["params"]["fields"] == ["shift"]
    assert content["params"]["resource"] == {"name": "data", "type": "resources"}


def test_group_data_includes_conditions_when_set():
    preparer = GroupDataPreparer(
        group_name="by_shift",
        fields=["shift"],
        resource=_RESOURCE,
        conditions=[CustomReportConditions.IS_PDF],
    )
    assert preparer.build_content()["conditions"] == ["is_pdf"]
