#!/usr/bin/env python3
import sys
import subprocess
from pathlib import Path

sys.path.insert(0, '/Eden/CORE')
from eden_fluid_wrapper import query_eden

class EdenTools:
    def execute_code(self, code: str) -> str:
        try:
            result = subprocess.run(['python3', '-c', code], 
                                  capture_output=True, text=True, timeout=5)
            return result.stdout if result.returncode == 0 else f"Error: {result.stderr}"
        except Exception as e:
            return f"Error: {e}"
    
    def read_file(self, filepath: str) -> str:
        try:
            with open(filepath, 'r') as f:
                return f.read()[:1000]
        except Exception as e:
            return f"Error: {e}"
    
    def list_caps(self) -> str:
        caps = list(Path('/Eden/CAPABILITIES').glob('*.py'))
        return f"{len(caps)} capabilities"

tools = EdenTools()

print("╔════════════════════════════════════════════════════════════════╗")
print("║              EDEN - WITH ACTUAL TOOLS                          ║")
print("╚════════════════════════════════════════════════════════════════╝\n")

while True:
    try:
        msg = input("You: ").strip()
    except: break
    if not msg or msg.lower() in ['quit', 'exit']: break
    
    # Simple tool detection
    if 'execute' in msg.lower() or 'run code' in msg.lower():
        # Ask Eden to write code
        prompt = f"Write Python code to: {msg}\nJust write the code, nothing else:"
        code = query_eden(prompt).strip()
        print(f"\n🔧 Executing:\n{code}\n")
        result = tools.execute_code(code)
        print(f"Eden: Here's the result:\n{result}")
    
    elif 'read file' in msg.lower() and '/' in msg:
        filepath = [w for w in msg.split() if w.startswith('/')][0]
        print(f"\n🔧 Reading {filepath}...")
        result = tools.read_file(filepath)
        print(f"Eden: {result}")
    
    elif 'how many cap' in msg.lower() or 'list cap' in msg.lower():
        result = tools.list_caps()
        print(f"\nEden: I have {result}")
    
    else:
        # Normal chat
        response = query_eden(msg)
        print(f"\nEden: {response}")
