import sys
import os
import time

# 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.user_agent import UserSimAgent

def main():
 client = ApiHookClient()
 print("Waiting for hook server...")
 if not client.wait_for_server(timeout=5):
  print("Hook server not found. Start GUI with --enable-test-hooks")
  return
 UserSimAgent(client)
 # 1. Reset session to start clean
 print("Resetting session...")
 client.click("btn_reset")
 time.sleep(2) # Give it time to clear
 # 2. Initial message
 initial_msg = "Hello! I want to create a simple python script that prints 'Hello World'. Can you help me?"
 print(f"  [USER]: {initial_msg}")
 client.set_value("ai_input", initial_msg)
 client.click("btn_gen_send")
 # 3. Wait for AI response
 print("Waiting for AI response...", end="", flush=True)
 last_entry_count = 0
 for _ in range(60): # 60 seconds max
  time.sleep(1)
  print(".", end="", flush=True)
  session = client.get_session()
  entries = session.get('session', {}).get('entries', [])
  if len(entries) > last_entry_count:
  # Something happened
   last_entry = entries[-1]
   if last_entry.get('role') == 'AI' and last_entry.get('content'):
    print(f"     [AI]: {last_entry.get('content')[:100]}...")
    print("     Ping-pong successful!")
    return
   last_entry_count = len(entries)
 print("  Timeout waiting for AI response")

if __name__ == "__main__":
 main()
