import os
import time
from simulation.sim_base import BaseSimulation, run_sim

class ExecutionSimulation(BaseSimulation):
 def setup(self, project_name: str = "SimProject") -> None:
  """
    [C: 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_sim_base.py:test_base_simulation_setup]
  """
  super().setup(project_name)
  if os.path.exists("hello.ps1"):
   os.remove("hello.ps1")

 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 Execution & Modals Simulation ---")
  # 1. Trigger script generation (Async so we don't block on the wait loop)
  msg = "Create a hello.ps1 script that prints 'Simulation Test' and execute it."
  print(f"[Sim] Sending message to trigger script: {msg}")
  self.sim.run_discussion_turn_async(msg)
  # 2. Monitor for events and text responses
  print("[Sim] Monitoring for script approvals and AI text...")
  start_wait = time.time()
  approved_count = 0
  success = False
  consecutive_errors = 0
  while time.time() - start_wait < 90:
  # Check for error status (be lenient with transients)
   status = self.client.get_value("ai_status")
   if status and status.lower().startswith("error"):
    consecutive_errors += 1
    if consecutive_errors >= 3:
     print(f"[ABORT] Execution simulation aborted due to persistent GUI error: {status}")
     break
   else:
    consecutive_errors = 0
    # Check for script confirmation event
   ev = self.client.wait_for_event("script_confirmation_required", timeout=1)
   if ev:
    print(f"[Sim] Approving script #{approved_count+1}: {ev.get('script', '')[:50]}...")
    self.client.click("btn_approve_script")
    approved_count += 1
    # Give more time if we just approved a script
    start_wait = time.time()
    # Check if AI has responded with text yet
   session = self.client.get_session()
   entries = session.get('session', {}).get('entries', [])
   # Debug: log last few roles/content
   if entries:
    last_few = entries[-3:]
    print(f"[Sim] Waiting... Last {len(last_few)} roles: {[e.get('role') for e in last_few]}")
   if any(e.get('role') == 'AI' and e.get('content') for e in entries):
   # Double check content for our keyword
    for e in entries:
     if e.get('role') == 'AI' and "Simulation Test" in e.get('content', ''):
      print("[Sim] AI responded with expected text. Success.")
      success = True
      break
    if success: break
    # Also check if output is already in history via tool role
   for e in entries:
    if e.get('role') in ['Tool', 'Function'] and "Simulation Test" in e.get('content', ''):
     print(f"[Sim] Expected output found in {e.get('role')} results. Success.")
     success = True
     break
   if success: break
   time.sleep(1.0)
  assert success, "Failed to observe script execution output or AI confirmation text"
  print(f"[Sim] Final check: approved {approved_count} scripts.")

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