"""Developer Tools for WebDrop Bridge.

Uses Qt WebEngine's built-in inspector wiring instead of the remote debugging
HTTP endpoint. This avoids localhost/port timing issues that can differ between
development runs and frozen MSI installations.
"""

import logging
from typing import Any

from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import QVBoxLayout, QWidget

logger = logging.getLogger(__name__)

__all__ = ["DeveloperToolsWidget"]


class DeveloperToolsWidget(QWidget):
    """Embedded Chromium Developer Tools Inspector.

    Attaches a dedicated DevTools page directly to the inspected web page.

    Features:
    - Real HTML/CSS Inspector with live editing
    - Full JavaScript Console with all DevTools features
    - Network monitoring
    - Performance profiling
    - Storage inspection
    - All standard Chromium DevTools features
    """

    def __init__(self, web_view: Any) -> None:
        """Initialize Developer Tools.

        Args:
            web_view: The QWebEngineView to debug
        """
        super().__init__()
        self.web_view = web_view

        # Create layout
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        # Create WebEngineView for DevTools UI
        self.dev_tools_view = QWebEngineView()
        layout.addWidget(self.dev_tools_view)

        # Directly attach DevTools to the existing page.
        # This is more robust than relying on the remote debugging HTTP endpoint.
        self.dev_tools_view.page().setInspectedPage(self.web_view.page())
        logger.info("Developer Tools attached via QWebEnginePage.setInspectedPage")
