import pytest
from typing import Any
import time
import sys
import os

# Ensure project root is in path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))

from api_hook_client import ApiHookClient
from simulation.sim_context import ContextSimulation
from simulation.sim_ai_settings import AISettingsSimulation
from simulation.sim_tools import ToolsSimulation
from simulation.sim_execution import ExecutionSimulation

@pytest.mark.integration
def test_context_sim_live(live_gui: Any) -> None:
 """Run the Context & Chat simulation against a live GUI."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=10)
 sim = ContextSimulation(client)
 sim.setup("LiveContextSim")
 client.set_value('current_provider', 'gemini_cli')
 client.set_value('gcli_path', f'"{sys.executable}" "{os.path.abspath("tests/mock_gemini_cli.py")}"')
 client.set_value('auto_add_history', True)
 sim.run() # Ensure history is updated via the async queue
 time.sleep(2)
 sim.teardown()

@pytest.mark.integration
def test_ai_settings_sim_live(live_gui: Any) -> None:
 """Run the AI Settings simulation against a live GUI."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=10)
 sim = AISettingsSimulation(client)
 sim.setup("LiveAISettingsSim")
 client.set_value('current_provider', 'gemini_cli')
 client.set_value('gcli_path', f'"{sys.executable}" "{os.path.abspath("tests/mock_gemini_cli.py")}"') # Expect gemini_cli as the provider
 client.set_value('auto_add_history', True)
 assert client.get_value('current_provider') == 'gemini_cli'
 sim.run()
 sim.teardown()

@pytest.mark.integration
def test_tools_sim_live(live_gui: Any) -> None:
 """Run the Tools & Search simulation against a live GUI."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=10)
 sim = ToolsSimulation(client)
 sim.setup("LiveToolsSim")
 client.set_value('current_provider', 'gemini_cli')
 client.set_value('gcli_path', f'"{sys.executable}" "{os.path.abspath("tests/mock_gemini_cli.py")}"')
 client.set_value('auto_add_history', True)
 sim.run() # Ensure history is updated via the async queue
 time.sleep(2)
 sim.teardown()
@pytest.mark.integration
def test_execution_sim_live(live_gui: Any) -> None:
 """Run the Execution & Modals simulation against a live GUI."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=10)
 sim = ExecutionSimulation(client)
 sim.setup("LiveExecutionSim")
 # Enable manual approval to test modals
 client.set_value('manual_approve', True)
 client.set_value('current_provider', 'gemini_cli')
 client.set_value('gcli_path', f'"{sys.executable}" "{os.path.abspath("tests/mock_gemini_cli.py")}"')
 client.set_value('auto_add_history', True)
 sim.run()
 time.sleep(2)
 sim.teardown()