"""Parser for ``<TableField>`` elements in DMSContent responses."""
import xml.etree.ElementTree as ET

from ecmind_blue_client.ecm.parser._dms_content_object_row_field import (
    DmsContentObjectRowField,
)


class DmsContentObjectTableField:
    """Represents a ``<TableField>`` element within ``<TableFields>`` of a HOL ``<Object>``.

    A table field holds a grid of values (rows × columns). Each row is parsed into
    a mapping of ``{internal_name: DmsContentObjectRowField}``.
    """

    def __init__(self, node: ET.Element):
        self._node = node

        column_nodes = self._node.findall("Columns/Column")
        rows_nodes = self._node.findall("Row")

        self._rows: list[dict[str, DmsContentObjectRowField]] = []

        for row_node in rows_nodes:
            row_id = int(row_node.attrib["id"])
            value_nodes = row_node.findall("Value")
            fields: dict[str, DmsContentObjectRowField] = {}
            for i, value_node in enumerate(value_nodes):
                field = DmsContentObjectRowField(column_nodes[i], value_node, row_id)
                fields[field.internal_name] = field
            self._rows.append(fields)

    @property
    def internal_name(self) -> str:
        """Internal (programmatic) name of this table field."""
        return self._node.get("internal_name", "")

    @property
    def name(self) -> str:
        """Display name of this table field."""
        return self._node.get("name", "")

    @property
    def ostype(self) -> str:
        """Object storage type string of this table field."""
        return self._node.get("ostype", "")

    @property
    def osguid(self) -> str:
        """Object storage GUID of this table field."""
        return self._node.get("osguid", "")

    @property
    def dbname(self) -> str:
        """Database column name backing this table field."""
        return self._node.get("dbname", "")

    @property
    def datatype(self) -> str:
        """Data type string of this table field (e.g. ``LIST``)."""
        return self._node.get("datatype", "")

    @property
    def type(self) -> str:
        """Field type string as returned by the server."""
        return self._node.get("type", "")

    @property
    def visible(self) -> int:
        """Visibility flag: ``1`` if the field is visible in the UI, ``0`` otherwise."""
        return int(self._node.get("visible", "1"))

    @property
    def sortpos(self) -> int:
        """Sort position of this table field within its parent object type."""
        return int(self._node.get("sortpos", "0"))

    @property
    def object(self) -> str:
        """Reference to a linked object associated with this table field."""
        return self._node.get("object", "")

    @property
    def rows(self) -> list[dict[str, DmsContentObjectRowField]]:
        """Returns all data rows of this table field.

        Returns:
            list[dict[str, DmsContentObjectRowField]]: One dict per row, keyed by field internal name.
        """
        return self._rows
