"""Builder for ``<ConditionGroup>`` elements with AND/OR logic in DMSQuery XML."""
import xml.etree.ElementTree as ET

from ecmind_blue_client.ecm.builder._common import DmsQueryConditionGroupOperatorsT
from ecmind_blue_client.ecm.builder._dms_query_object_condition_group_or_condition_object import (
    DmsQueryObjectConditionGroupOrConditionObject,
)


class DmsQueryObjectConditionGroup(DmsQueryObjectConditionGroupOrConditionObject):
    """
    Class to create a condition group for the DMS query.

    Args:
        parent (ET.Element): The parent node of the condition group.
        operator (ConditionGroupOpteratorsT): The operator of the condition group.
    """

    def __init__(self, parent: ET.Element, operator: DmsQueryConditionGroupOperatorsT):
        self._parent = parent
        self._condition_group_node = ET.SubElement(parent, "FieldGroup", {"operator": operator})
        super().__init__(self._condition_group_node)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

    def field_group(self, operator: DmsQueryConditionGroupOperatorsT = "OR") -> "DmsQueryObjectConditionGroup":
        """Creates a nested ``<FieldGroup>`` inside this group for recursive AND/OR expressions.

        Args:
            operator (DmsQueryConditionGroupOperatorsT): Logical operator for the nested group
                (``"AND"`` or ``"OR"``). Defaults to ``"OR"``.

        Returns:
            DmsQueryObjectConditionGroup: The nested condition group.
        """
        return DmsQueryObjectConditionGroup(self._parent, operator)
