"""Icon-loader tests (pytest-qt, offscreen)."""

from __future__ import annotations

import pytest

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

from mgf.brand_qt.icons import ICON_NAMES, icon
from mgf.brand_qt.tokens import RAW

pytestmark = pytest.mark.gui


def _pixels(name: str, color: str | None = None) -> bytes:
    pm = icon(name, color=color, size=24).pixmap(24, 24)
    image = pm.toImage()
    return bytes(image.constBits())


def test_the_full_42_icon_set_loads(qapp) -> None:
    assert len(ICON_NAMES) == 42
    for name in sorted(ICON_NAMES):
        assert not icon(name).isNull(), name


def test_recolor_changes_rendered_pixels(qapp) -> None:
    assert _pixels("refresh", RAW.gold) != _pixels("refresh", RAW.slate)


def test_unknown_name_raises_value_error(qapp) -> None:
    with pytest.raises(ValueError, match="unknown brand icon"):
        icon("hamburger")


def test_cache_returns_the_same_instance(qapp) -> None:
    assert icon("home") is icon("home")
    assert icon("home") is not icon("home", color=RAW.teal)


def test_ratio_overrides_the_application_dpr(qtbot) -> None:
    """PAPER-03: a per-screen caller can pin the render ratio."""
    from mgf.brand_qt.icons import icon

    hi = icon("lock", ratio=2.0)
    lo = icon("lock", ratio=1.0)
    # The ratio governs the rendered source resolution: 24 pt at 2.0
    # carries a 48-device-pixel bitmap; at 1.0, 24.
    assert [(s.width(), s.height()) for s in hi.availableSizes()] == [(48, 48)]
    assert [(s.width(), s.height()) for s in lo.availableSizes()] == [(24, 24)]
    assert hi is not lo  # distinct cache entries per ratio


def test_logo_pixmap_variants(qtbot) -> None:
    """PAPER-05: the Cornerstone Orbit mark is an accessor, not a re-ship."""
    import pytest as _pytest

    from mgf.brand_qt.icons import logo_pixmap

    for variant in ("color", "mono", "on-dark"):
        pm = logo_pixmap(variant, size=32)
        assert not pm.isNull()
    with _pytest.raises(ValueError, match="unknown logo variant"):
        logo_pixmap("sepia")
