import pytest
from src import ai_client
from src.api_hook_client import ApiHookClient
import time
import os

def test_system_prompt_sim(live_gui):
 """
 
  
   Simulation test for system prompt settings.
   1. Wait for server.
   2. Verify initial state.
   3. Modify settings via API.
   4. Verify updates.
   5. Use 'Reset to Default' button via API.
   6. Verify restoration to default text.
 """
 _, gui_script = live_gui
 client = ApiHookClient()
 client.click("btn_reset")
 time.sleep(2)
 
 # 1. Use client.wait_for_server().
 assert client.wait_for_server(timeout=15), "Server failed to start in time"
 
 # Wait a bit for GUI to settle
 time.sleep(2)
 
 # 2. Verify initial state: get_value('use_default_base_prompt') should be True.
 initial_default = client.get_value('use_default_base_prompt')
 assert initial_default is True, f"Expected use_default_base_prompt to be True, got {initial_default}"
 
 # 3. Call client.set_value('use_default_base_prompt', False).
 client.set_value('use_default_base_prompt', False)
 
 # 4. Call client.set_value('base_system_prompt', 'Simulation Base Prompt').
 client.set_value('base_system_prompt', 'Simulation Base Prompt')
 
 # Wait for processing
 time.sleep(0.5)
 
 # 5. Verify the values were updated.
 assert client.get_value('use_default_base_prompt') is False
 assert client.get_value('base_system_prompt') == 'Simulation Base Prompt'
 
 # 7. In the test, call client.click('btn_reset_base_prompt')
 client.click('btn_reset_base_prompt')
 
 # Give it a moment to process the click
 time.sleep(1.0)
 
 # verify use_default_base_prompt becomes False (Reset to Default sets it to False but copies the text)
 # Wait, if it was already False, it stays False.
 assert client.get_value('use_default_base_prompt') is False
 
 # 8. Verify the effective state is correct.
 # It should be back to ai_client._SYSTEM_PROMPT
 current_prompt = client.get_value('base_system_prompt')
 assert current_prompt == ai_client._SYSTEM_PROMPT, f"Prompt not reset. Got: {current_prompt[:50]}..."
 
 # 9. Verify 'Show Diff' modal
 initial_modal = client.get_value('show_base_prompt_diff_modal')
 assert initial_modal is False
 
 client.click('btn_show_base_prompt_diff')
 time.sleep(0.5)
 assert client.get_value('show_base_prompt_diff_modal') is True
 
 # Close it
 client.set_value('show_base_prompt_diff_modal', False)
 assert client.get_value('show_base_prompt_diff_modal') is False