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

@pytest.fixture
def mock_gui():
 gui = gui_2.App()
 gui.project = {
  'discussion': {
   'active': 'main',
   'discussions': {
    'main': {'history': []},
    'main_take_1': {'history': []},
    'other_topic': {'history': []}
   }
  }
 }
 gui.active_discussion = 'main'
 gui.perf_profiling_enabled = False
 gui.is_viewing_prior_session = False
 gui._get_discussion_names = lambda: ['main', 'main_take_1', 'other_topic']
 return gui

def test_discussion_tabs_rendered(mock_gui):
 with patch('src.gui_2.imgui') as mock_imgui, \
  patch('src.gui_2.imscope') as mock_imscope, \
  patch('src.imgui_scopes.imgui', new=mock_imgui), \
  patch('src.app_controller.AppController.active_project_root', new_callable=PropertyMock, return_value='.'):
  
  # 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

  # We expect a combo box for base discussion
  mock_imgui.begin_combo.return_value = True
  mock_imgui.selectable.return_value = (False, False)
  
  # We expect a tab bar for takes
  mock_imgui.begin_tab_bar.return_value = True
  mock_imgui.begin_tab_item.return_value = (True, True)
  mock_imgui.input_text.return_value = (False, "")
  mock_imgui.input_text_multiline.return_value = (False, "")
  mock_imgui.checkbox.return_value = (False, False)
  mock_imgui.input_int.return_value = (False, 0)
  mock_imgui.drag_int.return_value = (False, 0)
  
  mock_clipper = MagicMock()
  mock_clipper.step.return_value = False
  mock_imgui.ListClipper.return_value = mock_clipper
  
  gui_2.render_discussion_panel(mock_gui)
  
  mock_imgui.begin_combo.assert_called_once_with("##disc_sel", 'main')
  mock_imgui.begin_tab_bar.assert_called_once_with('discussion_takes_tabs')
  
  calls = [c[0][0] for c in mock_imscope.tab_item.call_args_list]
  assert 'Original###main' in calls
  assert 'Take 1###main_take_1' in calls
  assert 'Synthesis###Synthesis' in calls
