import pytest
from unittest.mock import MagicMock, patch, mock_open
from src.gui_2 import App
from src import models

@pytest.fixture
def mock_app():
 with (
  patch('src.app_controller.AppController.load_config', return_value={
   "ai": {"provider": "gemini", "model": "model-1"},
   "projects": {"paths": [], "active": ""},
   "gui": {"show_windows": {}}
  }),
  patch('src.project_manager.load_project', return_value={}),
  patch('src.project_manager.migrate_from_legacy_config', return_value={}),
  patch('src.project_manager.save_project'),
  patch('src.session_logger.open_session'),
  patch('src.session_logger.reset_session'),
  patch('src.app_controller.AppController._init_ai_and_hooks'),
  patch('src.app_controller.AppController._fetch_models'),
  patch('src.gui_2.App._load_fonts'),
  patch('src.gui_2.App._post_init')
 ):
  app = App()
  app.files = []
  app.context_files = []
  return app

def test_populate_auto_slices_basic(mock_app: App) -> None:
 f_item = models.FileItem(path="test.py")
 mock_outline = "[Class] MyClass (Lines 1-10)\n[Method] my_method (Lines 2-5)\n[Func] top_func (Lines 12-15)"
 
 with (
  patch('src.mcp_client.configure') as mock_conf,
  patch('src.mcp_client.py_get_code_outline', return_value=mock_outline) as mock_outline_tool,
  patch('builtins.open', mock_open(read_data="dummy content"))
 ):
  mock_app._populate_auto_slices(f_item)
  
  assert len(f_item.custom_slices) == 3
  s0 = f_item.custom_slices[0]
  assert s0["start_line"] == 1
  assert s0["end_line"] == 10
  assert s0["tag"] == 'auto-ast'
  assert s0["comment"] == "MyClass"

def test_add_selected_triggers_auto_slices(mock_app):
 f_mock = MagicMock()
 f_mock.path = "test.py"
 mock_app.files = [f_mock]
 mock_app._ui_picker_selected = {"test.py"}
 
 with patch('src.gui_2.imgui') as mock_imgui, \
      patch('src.gui_2.imscope') as mock_imscope:
  mock_imgui.begin_popup_modal.return_value = (True, True)
  mock_imgui.button.side_effect = lambda label, size=None: label == "Add Selected"
  mock_imgui.checkbox.return_value = (False, False)
  mock_imgui.begin_child.return_value = True
  mock_imgui.ImVec2 = MagicMock()
  mock_imgui.WindowFlags_ = MagicMock()
  
  # Setup imscope mocks to act as context managers
  mock_imscope.table.return_value.__enter__.return_value = True
  mock_imscope.tree_node_ex.return_value.__enter__.return_value = True
  mock_imscope.child.return_value.__enter__.return_value = True
  mock_imscope.window.return_value.__enter__.return_value = (True, True)
  
  with patch.object(mock_app, '_populate_auto_slices') as mock_populate:
   import src.gui_2
   src.gui_2.render_add_context_files_modal(mock_app)
   
   assert mock_populate.called
   assert len(mock_app.context_files) == 1
   assert mock_app.context_files[0].path == "test.py"

def test_add_all_triggers_auto_slices(mock_app):
 f_mock = MagicMock()
 f_mock.path = "test_all.py"
 mock_app.files = [f_mock]
 mock_app.context_files = []
 
 with patch('src.gui_2.imgui') as mock_imgui, \
      patch('src.gui_2.imscope') as mock_imscope:
  mock_imgui.button.side_effect = lambda label, size=None: label == "Add All##addall"
  mock_imgui.collapsing_header.return_value = True
  mock_imgui.begin_child.return_value = True
  mock_imgui.begin_table.return_value = True
  mock_imgui.tree_node_ex.return_value = False
  mock_imgui.checkbox.return_value = (False, False)
  mock_imgui.combo.return_value = (False, 0)
  mock_imgui.input_text.return_value = (False, "")
  mock_imgui.TableFlags_ = MagicMock()
  mock_imgui.TableColumnFlags_ = MagicMock()
  mock_imgui.TreeNodeFlags_ = MagicMock()
  
  # Setup imscope mocks to act as context managers
  mock_imscope.table.return_value.__enter__.return_value = True
  mock_imscope.tree_node_ex.return_value.__enter__.return_value = True
  mock_imscope.child.return_value.__enter__.return_value = True
  mock_imscope.window.return_value.__enter__.return_value = (True, True)
  
  with patch.object(mock_app, '_populate_auto_slices') as mock_populate:
   import src.gui_2
   src.gui_2.render_context_composition_panel(mock_app)
   
   assert mock_populate.called
   assert len(mock_app.context_files) == 1
   assert mock_app.context_files[0].path == "test_all.py"
