"""InfrastructureLayerPage widget — filtered registry view for a single layer.

Displays ServiceRows for every tool belonging to a given infrastructure
layer inside a scrollable area.
"""

try:
    from PySide6.QtGui import QFont
    from PySide6.QtWidgets import (
        QLabel,
        QScrollArea,
        QVBoxLayout,
        QWidget,
    )
    _HAS_QT = True
except ImportError:
    _HAS_QT = False

if _HAS_QT:
    try:
        from ai_lsc.ui.pages.service_row import ServiceRow
    except ImportError:
        ServiceRow = None

    class InfrastructureLayerPage(QWidget):
        """Filtered registry view for a single infrastructure layer.
        Displays ServiceRows for every tool belonging to the given layer."""

        def __init__(self, main_window, layer_name: str):
            super().__init__()
            self.main = main_window
            self.layer_name = layer_name
            layout = QVBoxLayout(self)

            lbl = QLabel(f"<b>Infrastructure Workspace -- {layer_name}</b>")
            lbl.setFont(QFont("Segoe UI", 14))
            layout.addWidget(lbl)

            lbl_desc = QLabel(
                f"Filtered registry view showing tools in the "
                f"{layer_name} abstraction stratum."
            )
            lbl_desc.setStyleSheet("color: #7f8c8d;")
            layout.addWidget(lbl_desc)

            scroll = QScrollArea()
            scroll.setWidgetResizable(True)
            scroll_content = QWidget()
            self.layer_layout = QVBoxLayout(scroll_content)

            grouped = self.main.registry_mgr.get_grouped_by_layer()
            layer_tools = grouped.get(layer_name, [])
            for t_id, meta in layer_tools:
                port = meta.get("launcher", {}).get("default_port")
                row = ServiceRow(self.main, t_id, port, meta)
                self.layer_layout.addWidget(row)

            self.layer_layout.addStretch()
            scroll.setWidget(scroll_content)
            layout.addWidget(scroll)

else:
    InfrastructureLayerPage = None
