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

@pytest.mark.parametrize("role", ["User", "AI"])
def test_render_discussion_panel_symbol_lookup(mock_app, role):
 # Mock imgui, mcp_client, and project_manager as requested
 with (
  patch('src.gui_2.imgui') as mock_imgui,
  patch('src.gui_2.imscope') as mock_imscope,
  patch('src.gui_2.mcp_client') as mock_mcp,
  patch('src.gui_2.project_manager') as mock_pm,
  patch('src.markdown_helper.imgui_md') as mock_md,
  patch('src.gui_2.theme') as mock_theme
 ):
  # Setup imscope mocks
  mock_imscope.window.return_value.__enter__.return_value = (True, True)
  mock_imscope.child.return_value.__enter__.return_value = True
  mock_imscope.table.return_value.__enter__.return_value = True
  mock_imscope.tree_node_ex.return_value.__enter__.return_value = True
  mock_imscope.tab_item.return_value.__enter__.return_value = (True, True)
  mock_imscope.style_color.return_value.__enter__.return_value = None
  mock_imscope.style_var.return_value.__enter__.return_value = None

  # Set up App instance state
  mock_app.perf_profiling_enabled = False
  mock_app.ai_status = "idle"
  mock_app.is_viewing_prior_session = False
  mock_app.active_discussion = "Default"
  mock_app.project = {"discussion": {"discussions": {"Default": {}}}}
  mock_app.disc_entries = [{"role": role, "content": "[Definition: MyClass from src/models.py (line 10)]", "collapsed": False, "read_mode": True}]
  mock_app.disc_roles = ["User", "AI"]
  mock_app.ui_files_base_dir = "."
  mock_app.active_track = None
  mock_app.ui_disc_new_name_input = ""
  mock_app.ui_auto_add_history = False
  mock_app.ui_disc_truncate_pairs = 10
  mock_app.ui_disc_new_role_input = ""
  mock_app._disc_entries_lock = MagicMock()
  mock_app._scroll_disc_to_bottom = False
  mock_app.ui_word_wrap = False
  mock_app.show_windows = {"Text Viewer": False}
  mock_app.text_viewer_title = ""
  mock_app.text_viewer_content = ""
  
  # Mock internal methods to avoid side effects
  mock_app._get_discussion_names = MagicMock(return_value=["Default"])
  
  # Mock imgui behavior to reach the entry rendering loop
  mock_imgui.collapsing_header.return_value = True
  mock_imgui.begin_combo.return_value = False
  mock_imgui.begin_child.return_value = True
  mock_imgui.checkbox.return_value = (False, False)
  mock_imgui.input_text.side_effect = lambda label, value, *args, **kwargs: (False, value)
  mock_imgui.input_text_multiline.side_effect = lambda label, value, *args, **kwargs: (False, value)
  mock_imgui.input_int.side_effect = lambda label, value, *args, **kwargs: (False, value)
  mock_imgui.drag_int.side_effect = lambda label, value, *args, **kwargs: (False, value)
  mock_imgui.get_cursor_start_pos.return_value = mock_imgui.ImVec2(0,0)

  # Mock button click for the [Source] button
  def button_side_effect(label, *args, **kwargs):
   if "[Source]##0_0" in label:
    return True
   return False
  mock_imgui.button.side_effect = button_side_effect
  
  # Mock mcp_client.read_file return value
  mock_mcp.read_file.return_value = "class MyClass:\n pass"
  
  # Execute the panel rendering
  gui_2.render_discussion_panel(mock_app)
  
  # Assertions
  mock_mcp.read_file.assert_called_with("src/models.py")
  assert mock_app.text_viewer_title == "src/models.py"
  assert mock_app.text_viewer_content == "class MyClass:\n pass"
  assert mock_app.show_windows.get("Text Viewer") is True
