"""
ANTI-SIMPLIFICATION: These tests verify the complex UI state management for the MMA Orchestration features.
They MUST NOT be simplified. They ensure that track proposals, worker spawning, and AI streams are correctly represented in the GUI.
"""
from unittest.mock import patch
import time
from src.gui_2 import App


def test_mma_ui_state_initialization(app_instance: App) -> None:
 """Verifies that the new MMA UI state variables are initialized correctly."""
 assert hasattr(app_instance, "ui_epic_input")
 assert hasattr(app_instance, "proposed_tracks")
 assert hasattr(app_instance, "_show_track_proposal_modal")
 assert hasattr(app_instance, "mma_streams")
 assert app_instance.ui_epic_input == ""
 assert app_instance.proposed_tracks == []
 assert app_instance._show_track_proposal_modal is False
 assert app_instance.mma_streams == {}


def test_process_pending_gui_tasks_show_track_proposal(app_instance: App) -> None:
 """Verifies that the 'show_track_proposal' action correctly updates the UI state."""
 mock_tracks = [{"id": "track_1", "title": "Test Track"}]
 task = {"action": "show_track_proposal", "payload": mock_tracks}
 app_instance._pending_gui_tasks.append(task)
 app_instance._process_pending_gui_tasks()
 assert app_instance.proposed_tracks == mock_tracks
 assert app_instance._show_track_proposal_modal is True


def test_cb_plan_epic_launches_thread(app_instance: App) -> None:
 """Verifies that _cb_plan_epic launches a thread and eventually queues a task."""
 app_instance.ui_epic_input = "Develop a new feature"
 app_instance.active_project_path = "test_project.toml"
 mock_tracks = [{"id": "track_1", "title": "Test Track"}]
 with (
  patch(
   "src.orchestrator_pm.get_track_history_summary",
   return_value="History summary",
  ) as mock_get_history,
  patch(
   "src.orchestrator_pm.generate_tracks", return_value=mock_tracks
  ) as mock_gen_tracks,
  patch("src.aggregate.build_file_items", return_value=[]),
 ):
  with (
   patch("src.project_manager.load_project", return_value={}),
   patch("src.project_manager.flat_config", return_value={}),
  ):
   app_instance._cb_plan_epic()
   max_wait = 5
   start_time = time.time()
   while (
    len(app_instance._pending_gui_tasks) < 3
    and time.time() - start_time < max_wait
   ):
    time.sleep(0.1)
   assert len(app_instance._pending_gui_tasks) >= 3
   actions = [t["action"] for t in app_instance._pending_gui_tasks]
   assert "handle_ai_response" in actions
   assert "show_track_proposal" in actions
   mock_get_history.assert_called_once()
   mock_gen_tracks.assert_called_once()


def test_process_pending_gui_tasks_mma_spawn_approval(app_instance: App) -> None:
 """Verifies that the 'mma_spawn_approval' action correctly updates the UI state."""
 task = {
  "action": "mma_spawn_approval",
  "ticket_id": "T1",
  "role": "Tier 3 Worker",
  "prompt": "Test Prompt",
  "context_md": "Test Context",
  "dialog_container": [None],
 }
 app_instance._pending_gui_tasks.append(task)
 app_instance._process_pending_gui_tasks()
 assert app_instance._pending_mma_spawn == task
 assert app_instance._mma_spawn_prompt == "Test Prompt"
 assert app_instance._mma_spawn_context == "Test Context"
 assert app_instance._mma_spawn_open is True
 assert app_instance._mma_spawn_edit_mode is False
 assert task["dialog_container"][0] is not None


def test_handle_ai_response_with_stream_id(app_instance: App) -> None:
 """Verifies routing to mma_streams."""
 task = {
  "action": "handle_ai_response",
  "payload": {
   "text": "Tier 1 Strategy Content",
   "stream_id": "Tier 1",
   "status": "Thinking...",
  },
 }
 app_instance._pending_gui_tasks.append(task)
 app_instance._process_pending_gui_tasks()
 assert app_instance.mma_streams.get("Tier 1") == "Tier 1 Strategy Content"
 assert app_instance.ai_status == "Thinking..."
 assert app_instance.ai_response == ""


def test_handle_ai_response_fallback(app_instance: App) -> None:
 """Verifies fallback to ai_response when stream_id is missing."""
 task = {
  "action": "handle_ai_response",
  "payload": {"text": "Regular AI Response", "status": "done"},
 }
 app_instance._pending_gui_tasks.append(task)
 app_instance._process_pending_gui_tasks()
 assert app_instance.ai_response == "Regular AI Response"
 assert app_instance.ai_status == "done"
 assert len(app_instance.mma_streams) == 0