"""
Early pytest plugin to mock Blender modules before any imports occur.
This ensures bpy is available when test modules are collected.
"""

import sys
from unittest.mock import Mock

# Mock bpy and related modules immediately when this plugin is loaded
def mock_blender_modules():
    """Create comprehensive mocks for all Blender modules"""
    
    # Create main bpy mock with all necessary attributes
    mock_bpy = Mock()
    mock_bmesh = Mock()
    
    # Create mock types
    mock_types = Mock()
    mock_types.Operator = Mock
    mock_types.Panel = Mock  
    mock_types.PropertyGroup = Mock
    mock_types.UIList = Mock
    mock_types.Scene = Mock()
    mock_types.NODE_MT_add = Mock()
    mock_types.VIEW3D_MT_add = Mock()
    mock_types.VIEW3D_MT_mesh_add = Mock()
    mock_types.NODE_MT_node = Mock()
    mock_types.IMAGE_MT_image = Mock()
    
    # Create mock props
    mock_props = Mock()
    mock_props.StringProperty = Mock
    mock_props.IntProperty = Mock
    mock_props.FloatVectorProperty = Mock
    mock_props.FloatProperty = Mock
    mock_props.BoolProperty = Mock
    mock_props.EnumProperty = Mock
    mock_props.CollectionProperty = Mock
    mock_props.PointerProperty = Mock
    
    # Create mock utils
    mock_utils = Mock()
    mock_utils.register_class = Mock()
    mock_utils.unregister_class = Mock()
    
    # Create mock app
    mock_app = Mock()
    mock_app.handlers = Mock()
    mock_app.handlers.load_post = []
    mock_app.handlers.persistent = lambda f: f
    
    # Create mock context
    mock_context = Mock()
    mock_context.window_manager = Mock()
    mock_context.scene = Mock()
    
    # Create mock data with images collection
    mock_data = Mock()
    mock_images = Mock()
    mock_data.images = mock_images
    
    # Attach all sub-modules to main bpy mock
    mock_bpy.types = mock_types
    mock_bpy.props = mock_props
    mock_bpy.utils = mock_utils
    mock_bpy.app = mock_app
    mock_bpy.context = mock_context
    mock_bpy.data = mock_data
    
    # Install mocks in sys.modules
    sys.modules['bpy'] = mock_bpy
    sys.modules['bmesh'] = mock_bmesh
    sys.modules['bpy.types'] = mock_types
    sys.modules['bpy.props'] = mock_props
    sys.modules['bpy.utils'] = mock_utils
    sys.modules['bpy.app'] = mock_app
    sys.modules['bpy.context'] = mock_context
    sys.modules['bpy.data'] = mock_data

# Execute mocking immediately when plugin loads
mock_blender_modules()

def pytest_configure(config):
    """Additional pytest configuration"""
    pass