"""Lightweight dialog to edit a node's name, description, and comments.

Used by the Workbench tree's "Edit Workspace" / "Edit Study Area" actions. It
edits only display metadata (name / description / comments) and never touches
provenance runs, so it is safe under the additive-only provenance model.

Q6b: both text fields are rich-text editors. The canonical DB columns keep
PLAIN text; the rich bodies ride in metadata sidecars (``emds_comments_html``
for the description — the field the ArcGIS comments editor mirrors — and
``comments_html`` for the QGIS-only comments field).
"""

from __future__ import annotations

from typing import Dict, Optional

from ..qt_compat import (
    QDialog,
    QDialogButtonBox,
    QFormLayout,
    QLineEdit,
    QVBoxLayout,
    exec_dialog,
)
from ..rich_comment_editor import RichCommentEditor
from ..translator import tr


class EditNodeDialog(QDialog):
    """Edit the name, description, and comments of a workspace or study area."""

    def __init__(
        self,
        title: str,
        name: str = "",
        description: str = "",
        comments: str = "",
        description_html: str = "",
        comments_html: str = "",
        parent=None,
    ) -> None:
        super().__init__(parent)
        self.setWindowTitle(title)
        self.setMinimumWidth(460)
        self.setMinimumHeight(420)

        layout = QVBoxLayout(self)
        form = QFormLayout()

        self._name_input = QLineEdit(name or "")
        form.addRow(tr("edit_node.label_name"), self._name_input)

        self._description_input = RichCommentEditor(self)
        self._description_input.set_content(description or "", description_html or "")
        form.addRow(tr("edit_node.label_description"), self._description_input)

        self._comments_input = RichCommentEditor(self)
        self._comments_input.set_content(comments or "", comments_html or "")
        form.addRow(tr("edit_node.label_comments"), self._comments_input)

        layout.addLayout(form)

        buttons = QDialogButtonBox(
            QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
        )
        buttons.accepted.connect(self.accept)
        buttons.rejected.connect(self.reject)
        layout.addWidget(buttons)

    def values(self) -> Dict[str, str]:
        """The edited fields: plain text plus the HTML sidecar bodies."""
        return {
            "name": self._name_input.text().strip(),
            "description": self._description_input.plain_text(),
            "description_html": self._description_input.html(),
            "comments": self._comments_input.plain_text(),
            "comments_html": self._comments_input.html(),
        }

    @staticmethod
    def edit(
        title: str,
        name: str = "",
        description: str = "",
        comments: str = "",
        description_html: str = "",
        comments_html: str = "",
        parent=None,
    ) -> Optional[Dict[str, str]]:
        """Show the dialog modally; return the edited fields or None."""
        dialog = EditNodeDialog(
            title,
            name,
            description,
            comments,
            description_html=description_html,
            comments_html=comments_html,
            parent=parent,
        )
        if exec_dialog(dialog) == QDialog.DialogCode.Accepted:
            return dialog.values()
        return None
