"""Builder for ``<ConditionObject>`` elements in DMSQuery XML."""
import xml.etree.ElementTree as ET
from typing import cast

from ecmind_blue_client.ecm.builder._common import (
    DmsQueryConditionGroupOperatorsT,
    DmsQueryFieldValueT,
    DmsQueryObjectIdentTypeT,
    find_node_or_create,
)
from ecmind_blue_client.ecm.builder._dms_query_fulltext import DmsQueryFulltext
from ecmind_blue_client.ecm.builder._dms_query_object_condition_group import (
    DmsQueryObjectConditionGroup,
)
from ecmind_blue_client.ecm.builder._dms_query_object_condition_group_or_condition_object import (
    DmsQueryObjectConditionGroupOrConditionObject,
)


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

    Args:
        parent (ET.Element): The parent node of the condition object.
        internal_name_or_type_id (str | int): The internal name or type id of the object.

    Returns:
        DmsQueryConditionObject: The condition object.
    """

    def __init__(self, parent: ET.Element, internal_name_or_type_id: str | int):
        self._parent_node = parent
        self._condition_object_node = find_node_or_create(self._parent_node, "ConditionObject", internal_name_or_type_id)

        super().__init__(self._condition_object_node)

    def __enter__(self):
        return self

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

    @property
    def object_type(self) -> DmsQueryObjectIdentTypeT | None:
        """DMS object type discriminator (``FOLDER``, ``REGISTER``, ``DOCUMENT``).

        Should be set when the display name is used to identify the type to avoid ambiguity.
        """
        return cast(DmsQueryObjectIdentTypeT | None, self._condition_object_node.attrib.get("type", None))

    @object_type.setter
    def object_type(self, value: DmsQueryObjectIdentTypeT):
        self._condition_object_node.attrib["type"] = value

    def field_group(self, operator: DmsQueryConditionGroupOperatorsT = "OR") -> DmsQueryObjectConditionGroup:
        """
        Creates a new condition group with the given operator.
        Args:
            operator (ConditionGroupOpteratorsT): The logical operator for the group (default is "OR").
        Returns:
            DmsQueryObjectConditionGroup: The condition group object.
        """
        return DmsQueryObjectConditionGroup(self._condition_object_node, operator)

    def fulltext(self, value: DmsQueryFieldValueT) -> DmsQueryFulltext:
        """Adds a ``<Fulltext>`` condition directly inside this condition object.

        Args:
            value (DmsQueryFieldValueT): The full-text search term. Accepts a plain string,
                a ``DmsQueryParamValue`` reference, or any other supported field value type.

        Returns:
            DmsQueryFulltext: The fulltext element; use to configure RetrievalWare search-engine attributes.
        """
        return DmsQueryFulltext(self._condition_object_node, value)
