import time
from simulation.sim_base import BaseSimulation, run_sim

class ToolsSimulation(BaseSimulation):
 def run(self) -> None:
  """
    [C: tests/conftest.py:kill_process_tree, tests/conftest.py:live_gui, tests/test_conductor_abort_event.py:test_conductor_abort_event_populated, tests/test_conductor_engine_v2.py:test_conductor_engine_dynamic_parsing_and_execution, tests/test_conductor_engine_v2.py:test_conductor_engine_run_executes_tickets_in_order, tests/test_extended_sims.py:test_ai_settings_sim_live, tests/test_extended_sims.py:test_context_sim_live, tests/test_extended_sims.py:test_execution_sim_live, tests/test_extended_sims.py:test_tools_sim_live, tests/test_external_editor_gui.py:get_vscode_processes, tests/test_external_editor_gui.py:test_vscode_launches_with_diff_view, tests/test_gui_custom_window.py:test_app_window_is_borderless, tests/test_headless_simulation.py:module, tests/test_headless_verification.py:test_headless_verification_error_and_qa_interceptor, tests/test_headless_verification.py:test_headless_verification_full_run, tests/test_mock_gemini_cli.py:run_mock, tests/test_orchestration_logic.py:test_conductor_engine_run, tests/test_parallel_execution.py:test_conductor_engine_pool_integration, tests/test_sim_ai_settings.py:test_ai_settings_simulation_run, tests/test_sim_context.py:test_context_simulation_run, tests/test_sim_execution.py:test_execution_simulation_run, tests/test_sim_tools.py:test_tools_simulation_run]
  """
  print("\n--- Running Tools Simulation ---")
  # 1. Trigger list_directory tool
  msg = "List the files in the current directory."
  print(f"[Sim] Sending message to trigger tool: {msg}")
  self.sim.run_discussion_turn(msg)
  time.sleep(2)
  # 2. Wait for AI to execute tool
  print("[Sim] Waiting for tool execution...")
  time.sleep(5) # Give it some time
  # 3. Verify Tool Log
  # We need a hook to get the tool log
  # In gui_2.py, there is _on_tool_log which appends to self._tool_log
  # We need a hook to read self._tool_log
  # 4. Trigger read_file tool
  msg = "Read the first 10 lines of aggregate.py."
  print(f"[Sim] Sending message to trigger tool: {msg}")
  self.sim.run_discussion_turn(msg)
  time.sleep(2)
  # 5. Wait and Verify
  print("[Sim] Waiting for tool execution...")
  time.sleep(5)
  session = self.client.get_session()
  entries = session.get('session', {}).get('entries', [])
  # Tool outputs are usually in the conversation history as 'Tool' role or similar
  tool_outputs = [e for e in entries if e.get('role') in ['Tool', 'Function']]
  print(f"[Sim] Found {len(tool_outputs)} tool outputs in history.")
  # Actually in Gemini history, they might be nested. 
  # But our GUI disc_entries list usually has them as separate entries or 
  # they are part of the AI turn.
  if not entries:
   print("[Sim] ERROR: No history entries found after tool execution.")
   return
  last_ai_msg = entries[-1]['content']
  print(f"[Sim] Final AI Response: {last_ai_msg[:100]}...")

if __name__ == "__main__":
 run_sim(ToolsSimulation)