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

class ToolsSimulation(BaseSimulation):
    def run(self):
        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)
        
        # 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)
        
        # 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.
        
        # Let's check if the AI mentions it in its response
        last_ai_msg = entries[-1]['content']
        print(f"[Sim] Final AI Response: {last_ai_msg[:100]}...")

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