import pytest
from unittest.mock import MagicMock, patch, PropertyMock
from src.hot_reloader import HotReloader, HotModule


def test_hot_module_dataclass_fields():
 hm = HotModule(name='test.module', file_path='/path/to/module.py', state_keys=['key1', 'key2'], delegation_targets=['func1', 'func2'])
 assert hm.name == 'test.module'
 assert hm.file_path == '/path/to/module.py'
 assert hm.state_keys == ['key1', 'key2']
 assert hm.delegation_targets == ['func1', 'func2']


def test_hot_reloader_register_and_get():
 HotReloader.HOT_MODULES.clear()
 hm = HotModule(name='test.module', file_path='/path/to/module.py')
 HotReloader.register(hm)
 assert 'test.module' in HotReloader.HOT_MODULES
 assert HotReloader.HOT_MODULES['test.module'] is hm


def test_hot_reloader_register_duplicate_raises():
 HotReloader.HOT_MODULES.clear()
 hm = HotModule(name='test.module', file_path='/path/to/module.py')
 HotReloader.register(hm)
 with pytest.raises(ValueError, match="already registered"):
  HotReloader.register(hm)


def test_hot_reloader_is_error_state():
 HotReloader.HOT_MODULES.clear()
 HotReloader.is_error_state = False
 assert HotReloader.is_error_state is False
 HotReloader.last_error = "Test error"
 HotReloader.is_error_state = True
 assert HotReloader.is_error_state is True


def test_reload_unknown_module_returns_false():
 HotReloader.HOT_MODULES.clear()
 mock_app = MagicMock()
 result = HotReloader.reload('unknown.module', mock_app)
 assert result is False
 assert HotReloader.last_error == "Module unknown.module not registered"
 assert HotReloader.is_error_state is True


def test_reload_success_clears_error_state():
 HotReloader.HOT_MODULES.clear()
 HotReloader.last_error = "Previous error"
 HotReloader.is_error_state = True
 hm = HotModule(name='test.module', file_path='/path/to/module.py', state_keys=['active_discussion'])
 HotReloader.register(hm)
 mock_app = MagicMock()
 with patch('importlib.reload') as mock_reload, \
         patch('importlib.import_module') as mock_import:
  mock_import.side_effect = Exception("Module does not exist")
  result = HotReloader.reload('test.module', mock_app)
  assert result is False
  assert HotReloader.last_error is not None
  HotReloader.HOT_MODULES.clear()


def test_reload_captures_and_restores_state_on_failure():
 HotReloader.HOT_MODULES.clear()
 hm = HotModule(name='test.module', file_path='/path/to/module.py', state_keys=['active_discussion'])
 HotReloader.register(hm)
 mock_app = MagicMock()
 mock_app.active_discussion = 'main'
 with patch('importlib.reload', side_effect=Exception("Reload failed")):
  result = HotReloader.reload('test.module', mock_app)
  assert result is False
  assert HotReloader.is_error_state is True


def test_reload_all_success():
 HotReloader.HOT_MODULES.clear()
 hm1 = HotModule(name='module1', file_path='/path/to/module1.py', state_keys=[])
 hm2 = HotModule(name='module2', file_path='/path/to/module2.py', state_keys=[])
 HotReloader.register(hm1)
 HotReloader.register(hm2)
 mock_app = MagicMock()
 with patch('importlib.reload') as mock_reload, \
         patch('importlib.import_module') as mock_import:
  mock_reload.return_value = None
  mock_import.return_value = MagicMock()
  result = HotReloader.reload_all(mock_app)
  assert result is True


def test_reload_all_partial_failure():
 HotReloader.HOT_MODULES.clear()
 hm1 = HotModule(name='module1', file_path='/path/to/module1.py', state_keys=[])
 HotReloader.register(hm1)
 mock_app = MagicMock()
 with patch('importlib.reload', side_effect=Exception("Fail")):
  result = HotReloader.reload_all(mock_app)
  assert result is False


class TestHotReloadTriggerIntegration:
 def test_trigger_hot_reload_calls_reload_all(self):
  HotReloader.HOT_MODULES.clear()
  hm = HotModule(name='src.gui_2', file_path='/path/gui_2.py', state_keys=['active_discussion'], delegation_targets=[])
  HotReloader.register(hm)
  mock_app = MagicMock()
  mock_app._hot_reload_error = None
  with patch.object(HotReloader, 'reload_all', return_value=True) as mock_reload_all:
   from src.gui_2 import App
   with patch.object(App, '_trigger_hot_reload', App._trigger_hot_reload.__wrapped__ if hasattr(App._trigger_hot_reload, '__wrapped__') else None):
    pass
  HotReloader.HOT_MODULES.clear()

 def test_hot_reload_error_state_tracked_in_app(self):
  from src.gui_2 import App
  with patch('src.gui_2.app_controller.AppController'):
   app = App.__new__(App)
   app._hot_reload_error = None
   assert app._hot_reload_error is None

 def test_keyboard_shortcut_check_in_gui_func(self):
  from src.gui_2 import App
  mock_imgui = MagicMock()
  mock_io = MagicMock()
  mock_io.key_ctrl = True
  mock_io.key_alt = True
  mock_imgui.get_io.return_value = mock_io
  mock_imgui.Key = MagicMock()
  mock_imgui.Key.r = 'mock_r_key'
  mock_imgui.is_key_down.side_effect = lambda k: k == 'mock_r_key'
  mock_app = MagicMock()
  mock_app._trigger_hot_reload = MagicMock(return_value=True)
  mock_app.perf_profiling_enabled = False
  mock_app.is_viewing_prior_session = False
  mock_app.ai_status = 'idle'
  mock_app.ui_crt_filter = False
  mock_app.show_windows = {}
  with patch('src.gui_2.imgui', mock_imgui), \
             patch('src.gui_2.theme') as mock_theme, \
             patch('src.gui_2.bg_shader') as mock_bg, \
             patch('src.gui_2.render_custom_title_bar'), \
             patch('src.gui_2.render_shader_live_editor'), \
             patch('src.gui_2.render_history_window'), \
             patch('src.gui_2.render_main_interface'):
   mock_bg.get_bg.return_value.enabled = False
   mock_theme.is_nerv_active.return_value = False
   App._gui_func(mock_app)
  mock_app._trigger_hot_reload.assert_called_once()

 def test_mma_global_controls_renders_reload_button(self):
  from src.gui_2 import App, render_mma_global_controls
  mock_imgui = MagicMock()
  mock_imgui.checkbox.return_value = (False, False)
  mock_imgui.ImVec4 = MagicMock(side_effect=lambda r, g, b, a: (float(r), float(g), float(b), float(a)))
  mock_imgui.ImVec2 = MagicMock(side_effect=lambda x, y: (float(x), float(y)))
  mock_app = MagicMock()
  mock_app.mma_step_mode = False
  mock_app.mma_status = 'idle'
  mock_app.controller = None
  mock_app.active_tier = None
  mock_app._pending_mma_spawns = []
  mock_app._pending_mma_approvals = []
  mock_app._pending_ask_dialog = False
  mock_app._trigger_hot_reload = MagicMock(return_value=True)
  mock_app._hot_reload_error = None
  mock_app.controller = MagicMock()
  mock_app.controller.engine = None
  with patch('src.gui_2.imgui', mock_imgui), \
             patch('src.gui_2.C_VAL', (1, 0.5, 0, 1)):
   render_mma_global_controls(mock_app)
  mock_imgui.button.assert_any_call("Reload GUI")