with open('eden_api_server.py', 'r') as f:
    content = f.read()

# Check if class already exists
if 'class FluidPhiArchitecture:' in content:
    print("Class already exists, skipping")
else:
    # Add the class right after imports, before any routes
    fluid_class = '''
# ============================================================================
# 🌀 FLUID PHI-FRACTAL ARCHITECTURE
# ============================================================================

class FluidPhiArchitecture:
    """Adaptive architecture that flows between reasoning modes"""
    
    def __init__(self):
        self.phi_state = 1.618
        
    def detect_optimal_mode(self, query):
        """Detect optimal reasoning mode"""
        q_lower = query.lower()
        
        # Computational
        if any(sig in q_lower for sig in ['calculate', '=', 'equation', 'solve', 
                                           'what is', 'how much', 'average', '+', '-', '*', '/']):
            return 'computational'
        
        # Causal
        if any(sig in q_lower for sig in ['cause', 'why', 'because', 'reason',
                                           'lead to', 'happen', 'result']):
            return 'causal'
        
        # Logical
        if any(sig in q_lower for sig in ['all', 'some', 'none', 'if', 'then',
                                           'implies', 'therefore', 'conclude']):
            return 'logical'
        
        return 'meta'
    
    def flow(self, query):
        """Flow into optimal mode"""
        mode = self.detect_optimal_mode(query)
        
        if mode == 'computational':
            enhanced = f"Solve this precisely: {query}"
        elif mode == 'causal':
            enhanced = f"""Analyze causally using phi-fractal layers:
Layer 5 (Chronos): When did events occur?
Layer 6 (Causalis): What directly caused what?
Layer 7 (ChainReasoning): What's the causal chain?
Layer 8 (CounterFactual): Would removing cause change effect?

{query}"""
        elif mode == 'logical':
            enhanced = f"Apply formal logic: {query}"
        else:
            enhanced = query
            
        return enhanced, mode

'''
    
    # Insert after imports
    app_route_pos = content.find('@app.route')
    content = content[:app_route_pos] + fluid_class + '\n' + content[app_route_pos:]
    
    with open('eden_api_server.py', 'w') as f:
        f.write(content)
    
    print("✅ Added FluidPhiArchitecture class")

