"""Builder for the ``<Fulltext>`` element with RetrievalWare search-engine attributes."""
import xml.etree.ElementTree as ET
from typing import cast

from ecmind_blue_client.ecm.builder._common import DmsQueryFieldValueT, DmsQueryFulltextModeT, write_field_value


class DmsQueryFulltext:
    """Wraps the ``<Fulltext>`` element and exposes RetrievalWare search-engine attributes as properties.

    RetrievalWare attributes are optional; omitting them uses the server-side defaults documented
    in the XSD (``MODE=PATTERN``, ``EXPANSION_LEVEL_PROPERTY=4``, etc.).
    """

    def __init__(self, parent: ET.Element, value: DmsQueryFieldValueT) -> None:
        """
        Args:
            parent (ET.Element): The parent XML element (e.g. ``<FulltextQuery>`` or ``<ConditionObject>``).
            value (DmsQueryFieldValueT): The full-text search term.
        """
        self._node = ET.SubElement(parent, "Fulltext")
        write_field_value(self._node, value)

    def __enter__(self):
        return self

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

    @property
    def mode(self) -> DmsQueryFulltextModeT | None:
        """Full-text search mode (``BOOLEAN``, ``PATTERN``, ``CONCEPT``). XSD default: ``PATTERN``."""
        return cast(DmsQueryFulltextModeT | None, self._node.attrib.get("MODE", None))

    @mode.setter
    def mode(self, value: DmsQueryFulltextModeT):
        self._node.attrib["MODE"] = value

    @property
    def expansion_level(self) -> int | None:
        """Word expansion level for thesaurus lookups (``EXPANSION_LEVEL_PROPERTY``). XSD default: ``4``."""
        val = self._node.attrib.get("EXPANSION_LEVEL_PROPERTY", None)
        return int(val) if val is not None else None

    @expansion_level.setter
    def expansion_level(self, value: int):
        self._node.attrib["EXPANSION_LEVEL_PROPERTY"] = str(value)

    @property
    def fuzzy_spell_half_words(self) -> bool | None:
        """Enable fuzzy spelling for half-words (``FUZZY_SPELL_HALF_WORDS``). XSD default: ``false``."""
        val = self._node.attrib.get("FUZZY_SPELL_HALF_WORDS", None)
        return val == "true" if val is not None else None

    @fuzzy_spell_half_words.setter
    def fuzzy_spell_half_words(self, value: bool):
        self._node.attrib["FUZZY_SPELL_HALF_WORDS"] = "true" if value else "false"

    @property
    def fuzzy_spell_threshold(self) -> int | None:
        """Fuzzy spelling similarity threshold (``FUZZY_SPELL_THRESHOLD``). XSD default: ``0``."""
        val = self._node.attrib.get("FUZZY_SPELL_THRESHOLD", None)
        return int(val) if val is not None else None

    @fuzzy_spell_threshold.setter
    def fuzzy_spell_threshold(self, value: int):
        self._node.attrib["FUZZY_SPELL_THRESHOLD"] = str(value)

    @property
    def max_fuzzy_spell(self) -> int | None:
        """Maximum number of fuzzy spelling hits (``MAX_FUZZY_SPELL_PROPERTY``). XSD default: ``15``."""
        val = self._node.attrib.get("MAX_FUZZY_SPELL_PROPERTY", None)
        return int(val) if val is not None else None

    @max_fuzzy_spell.setter
    def max_fuzzy_spell(self, value: int):
        self._node.attrib["MAX_FUZZY_SPELL_PROPERTY"] = str(value)

    @property
    def max_reg_expr(self) -> int | None:
        """Maximum number of regular expression expansions (``MAX_REG_EXPR_PROPERTY``). XSD default: ``4``."""
        val = self._node.attrib.get("MAX_REG_EXPR_PROPERTY", None)
        return int(val) if val is not None else None

    @max_reg_expr.setter
    def max_reg_expr(self, value: int):
        self._node.attrib["MAX_REG_EXPR_PROPERTY"] = str(value)

    @property
    def warn_max_reg_expr(self) -> bool | None:
        """Warn when the regular expression expansion limit is exceeded (``WARN_MAX_REG_EXPR_PROPERTY``). XSD default: ``false``."""
        val = self._node.attrib.get("WARN_MAX_REG_EXPR_PROPERTY", None)
        return val == "true" if val is not None else None

    @warn_max_reg_expr.setter
    def warn_max_reg_expr(self, value: bool):
        self._node.attrib["WARN_MAX_REG_EXPR_PROPERTY"] = "true" if value else "false"

    @property
    def word_expansion_limit(self) -> int | None:
        """Maximum number of word expansions (``WORD_EXPANSION_LIMIT_PROPERTY``). XSD default: ``20``."""
        val = self._node.attrib.get("WORD_EXPANSION_LIMIT_PROPERTY", None)
        return int(val) if val is not None else None

    @word_expansion_limit.setter
    def word_expansion_limit(self, value: int):
        self._node.attrib["WORD_EXPANSION_LIMIT_PROPERTY"] = str(value)
