"""Tests for property group update hooks."""

from types import ModuleType, SimpleNamespace
import importlib.util
import pathlib
import sys

import pytest


@pytest.fixture
def property_groups_module():
    """Import property_groups with minimal bpy stubs."""
    module_name = "src.properties.property_groups"
    original_modules = {
        name: sys.modules.get(name)
        for name in (module_name, "bpy", "bpy.props", "bpy.types", "src", "src.properties")
    }

    class TypesStub(SimpleNamespace):
        def __getattr__(self, name):
            value = type(name, (), {})
            setattr(self, name, value)
            return value

    bpy_stub = SimpleNamespace(
        props=SimpleNamespace(
            StringProperty=lambda **kwargs: None,
            IntProperty=lambda **kwargs: None,
            FloatVectorProperty=lambda **kwargs: None,
            FloatProperty=lambda **kwargs: None,
            BoolProperty=lambda **kwargs: None,
            EnumProperty=lambda **kwargs: None,
            CollectionProperty=lambda **kwargs: None,
            PointerProperty=lambda **kwargs: None,
        ),
        types=TypesStub(PropertyGroup=object),
    )

    sys.modules["bpy"] = bpy_stub
    sys.modules["bpy.props"] = bpy_stub.props
    sys.modules["bpy.types"] = bpy_stub.types

    src_module = sys.modules.get("src")
    if src_module is None:
        src_module = ModuleType("src")
        src_module.__path__ = [str(pathlib.Path(__file__).resolve().parents[3] / "src")]
        sys.modules["src"] = src_module

    props_package = sys.modules.get("src.properties")
    if props_package is None:
        props_package = ModuleType("src.properties")
        props_package.__path__ = [str(pathlib.Path(__file__).resolve().parents[3] / "src" / "properties")]
        sys.modules["src.properties"] = props_package

    spec = importlib.util.spec_from_file_location(
        module_name,
        pathlib.Path(__file__).resolve().parents[3] / "src" / "properties" / "property_groups.py",
    )
    module = importlib.util.module_from_spec(spec)
    assert spec.loader is not None
    sys.modules[module_name] = module
    spec.loader.exec_module(module)

    try:
        yield module
    finally:
        for name, value in original_modules.items():
            if value is None:
                sys.modules.pop(name, None)
            else:
                sys.modules[name] = value


def test_update_live_preview_no_scene_is_noop(property_groups_module):
    """Function should safely return when context has no scene."""
    result = property_groups_module.update_live_preview(SimpleNamespace(), SimpleNamespace())
    assert result is None


def test_update_live_preview_with_scene_noop(property_groups_module):
    """Even with props present, function should not modify state."""
    props = SimpleNamespace(is_updating=False)
    context = SimpleNamespace(scene=SimpleNamespace(text_texture_props=props))

    result = property_groups_module.update_live_preview(SimpleNamespace(), context)

    assert result is None
    assert props.is_updating is False
