"""ToolsTab widget — read-only tree view of the full tool registry.

Displays every registered tool grouped by its infrastructure layer,
showing tool name, level, layer, and role.
"""

try:
    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import (
        QLabel,
        QTreeWidget,
        QTreeWidgetItem,
        QVBoxLayout,
        QWidget,
    )
    _HAS_QT = True
except ImportError:
    _HAS_QT = False

if _HAS_QT:

    class ToolsTab(QWidget):
        """Read-only tree view of the full tool registry, grouped by layer."""

        def __init__(self, main_window):
            super().__init__()
            self.main = main_window
            layout = QVBoxLayout(self)
            layout.addWidget(QLabel(
                "<b>Registry Tool Surface (13-Layer Architecture)</b>"
            ))

            self.tree = QTreeWidget()
            self.tree.setColumnCount(4)
            self.tree.setHeaderLabels(["Tool", "Level", "Layer", "Role"])
            layout.addWidget(self.tree)

        def refresh(self):
            self.tree.clear()
            grouped = self.main.registry_mgr.get_grouped_by_layer()
            for layer, tools in grouped.items():
                layer_item = QTreeWidgetItem([layer])
                self.tree.addTopLevelItem(layer_item)
                for t_id, meta in tools:
                    lvl = f"L{meta.get('level', 0)}"
                    item = QTreeWidgetItem([
                        meta.get("name", t_id),
                        lvl,
                        meta.get("layer", ""),
                        meta.get("role", ""),
                    ])
                    item.setData(0, Qt.UserRole, t_id)
                    layer_item.addChild(item)
            self.tree.expandAll()

else:
    ToolsTab = None
