"""Dynamic-property helper tests (pytest-qt, offscreen)."""

from __future__ import annotations

import pytest

pytest.importorskip("PySide6")
pytest.importorskip("pytestqt")

from PySide6.QtWidgets import QLabel, QPushButton

from mgf.brand_qt.tokens import FONT_MONO
from mgf.brand_qt.widgets import apply_label_class, set_mgf

pytestmark = pytest.mark.gui


def test_set_mgf_sets_prefixed_properties(qtbot) -> None:
    button = QPushButton("Go")
    qtbot.addWidget(button)
    set_mgf(button, Variant="accent", Chrome="true")
    assert button.property("mgfVariant") == "accent"
    assert button.property("mgfChrome") == "true"


def test_set_mgf_rejects_an_unknown_key_with_the_prefix_hint(qtbot) -> None:
    """PAPER-10 seeded defect: the exact call that shipped an unstyled shell."""
    label = QLabel("chrome")
    qtbot.addWidget(label)
    with pytest.raises(ValueError, match="pass the bare key"):
        set_mgf(label, mgfChrome="true")
    assert label.property("mgfMgfChrome") is None  # nothing half-applied


def test_set_mgf_rejects_an_unknown_value(qtbot) -> None:
    """PAPER-10's value half (court round C-1: Class='strongLabl' stayed green)."""
    label = QLabel("safety")
    qtbot.addWidget(label)
    with pytest.raises(ValueError, match="unknown value 'strongLabl'"):
        set_mgf(label, Class="strongLabl")


def test_set_mgf_accepts_the_entire_published_vocabulary(qtbot) -> None:
    """False-positive pin: every key/value the stylesheet selects on passes."""
    from mgf.brand_qt.qss import MGF_PROPS

    for key, values in MGF_PROPS.items():
        for value in sorted(values):
            widget = QPushButton(value)
            qtbot.addWidget(widget)
            set_mgf(widget, **{key: value})
            assert widget.property(f"mgf{key}") == value


def test_apply_label_class_tags_and_eyebrow_gets_mono_letterspacing(qtbot) -> None:
    label = QLabel("workspace")
    qtbot.addWidget(label)
    apply_label_class(label, "hint")
    assert label.property("mgfClass") == "hint"
    eyebrow = QLabel("workspace")
    qtbot.addWidget(eyebrow)
    apply_label_class(eyebrow, "eyebrow")
    assert eyebrow.property("mgfClass") == "eyebrow"
    assert eyebrow.font().family() == FONT_MONO
    assert eyebrow.font().letterSpacing() > 100
