from __future__ import annotations
from unittest.mock import patch, MagicMock

from src import gui_2
from src.gui_2 import App


def _make_app(**kwargs):
 app = MagicMock(spec=App)
 app.mma_streams = kwargs.get("mma_streams", {})
 app.mma_tier_usage = kwargs.get("mma_tier_usage", {
  "Tier 1": {"input": 0, "output": 0, "model": "gemini-3.1-pro-preview"},
  "Tier 2": {"input": 0, "output": 0, "model": "gemini-3-flash-preview"},
  "Tier 3": {"input": 0, "output": 0, "model": "gemini-2.5-flash-lite"},
  "Tier 4": {"input": 0, "output": 0, "model": "gemini-2.5-flash-lite"},
 })
 app.tracks = kwargs.get("tracks", [])
 app.active_track = kwargs.get("active_track", None)
 app.active_tickets = kwargs.get("active_tickets", [])
 app.mma_status = kwargs.get("mma_status", "idle")
 app.active_tier = kwargs.get("active_tier", None)
 app.mma_step_mode = kwargs.get("mma_step_mode", False)
 app._pending_mma_spawn = kwargs.get("_pending_mma_spawn", None)
 app._pending_mma_approval = kwargs.get("_pending_mma_approval", None)
 app._pending_ask_dialog = kwargs.get("_pending_ask_dialog", False)
 app.perf_profiling_enabled = False
 app.ui_new_track_name = ""
 app.ui_new_track_desc = ""
 app.ui_new_track_type = "feature"
 app.ui_conductor_setup_summary = ""
 app.ui_epic_input = ""
 app._show_add_ticket_form = False
 app.ui_new_ticket_id = ""
 app.ui_new_ticket_desc = ""
 app.ui_new_ticket_target = ""
 app.ui_new_ticket_deps = ""
 app._tier_stream_last_len = {}
 app.is_viewing_prior_session = False
 mock_engine = MagicMock()
 mock_engine._pause_event = MagicMock()
 mock_engine._pause_event.is_set.return_value = False
 mock_controller = MagicMock()
 mock_controller.engine = mock_engine
 app.controller = mock_controller
 return app


def _make_imgui_mock():
 m = MagicMock()
 m.begin_table.return_value = False
 m.begin_child.return_value = False
 m.checkbox.return_value = (False, False)
 m.input_text.side_effect = lambda label, value, *args, **kwargs: (False, value)
 m.input_text_multiline.side_effect = lambda label, value, *args, **kwargs: (False, value)
 m.combo.side_effect = lambda label, current_item, items, *args, **kwargs: (False, current_item)
 m.collapsing_header.return_value = False
 m.ImVec2.return_value = MagicMock()
 return m


class TestMMADashboardStreams:

 def test_tier1_renders_stream_content(self):
  """_render_tier_stream_panel for Tier 1 must call text_wrapped with the stream content."""
  app = _make_app(mma_streams={"Tier 1": "hello"})
  imgui_mock = _make_imgui_mock()
  imgui_mock.begin_child.return_value = True
  with patch("src.gui_2.imgui", imgui_mock), \
       patch("src.gui_2.imscope") as mock_imscope, \
       patch("src.gui_2.render_selectable_label") as mock_render:
   # 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

   gui_2.render_tier_stream_panel(app, "Tier 1", "Tier 1")
  
  mock_render.assert_called_with(app, 'stream_Tier 1', 'hello', width=-1, multiline=True, height=0)

 def test_tier3_renders_worker_subheaders(self):
  """_render_tier_stream_panel for Tier 3 must render a sub-header for each worker stream key."""
  app = _make_app(mma_streams={
   "Tier 3 (Worker): T-001": "out1",
   "Tier 3 (Worker): T-002": "out2",
  })
  imgui_mock = _make_imgui_mock()
  imgui_mock.begin_child.return_value = True
  with patch("src.gui_2.imgui", imgui_mock), \
       patch("src.gui_2.imscope") as mock_imscope, \
       patch("src.gui_2.render_selectable_label"):
   # 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

   gui_2.render_tier_stream_panel(app, "Tier 3", None)
  text_args = " ".join(str(c) for c in imgui_mock.text.call_args_list)
  assert "T-001" in text_args, "imgui.text not called with 'T-001' worker sub-header"
  assert "T-002" in text_args, "imgui.text not called with 'T-002' worker sub-header"