from typing import Any, List, Optional, Union

from ..report_conditions import CustomReportConditions
from ..report_element import CustomReportElement
from ..report_named_resource import CustomReportNamedResource


class GroupDataPreparer(CustomReportElement):
    """Groups rows in a resource by specified fields."""

    def __init__(
        self,
        group_name: str,
        fields: List[str],
        resource: Union[CustomReportNamedResource, Any],
        conditions: Optional[List[CustomReportConditions]] = None,
    ):
        """Creates a GroupDataPreparer.

        Args:
            group_name: Name assigned to the resulting group.
            fields: Fields used as grouping keys.
            resource: Source resource to group.
            conditions: Export-format conditions that control when this preparer is applied.
        """
        super().__init__(name="group_data", conditions=conditions)
        self.add_param("group_name", group_name)
        self.add_param("fields", fields)
        self.add_param("resource", resource)
