"""
Integrate Autonomous Revenue into Eden's main recursive loop
"""
import sys
sys.path.append('/Eden/CORE/phi_fractal')
sys.path.append('/Eden/CORE')

print("\n" + "="*70)
print("🔧 INTEGRATING REVENUE META-CAP INTO MAIN LOOP")
print("="*70)
print()

# Read current main loop
with open('/Eden/CORE/eden_with_agi_priority_fixed.py', 'r') as f:
    main_code = f.read()

# Check if already integrated
if 'AutonomousRevenue' in main_code:
    print("✅ Revenue meta-cap already integrated!")
else:
    print("Adding revenue meta-cap to improvement cycle...")
    
    # Add import at top
    import_line = "from eden_META_autonomous_revenue import AutonomousRevenue\n"
    
    # Find where to add import (after other imports)
    import_pos = main_code.find("class EdenWithAGI")
    modified_code = main_code[:import_pos] + import_line + main_code[import_pos:]
    
    # Add revenue initialization in __init__
    init_pos = modified_code.find("self.agi_queue = ['mathematical_native',")
    init_addition = """
        # Revenue generation
        try:
            self.revenue_system = AutonomousRevenue()
            print("💰 Revenue system loaded")
        except Exception as e:
            self.revenue_system = None
            print(f"⚠️ Revenue system not loaded: {e}")
        self.last_revenue_cycle = 0
        """
    
    modified_code = modified_code[:init_pos] + modified_code[init_pos:init_pos+100] + init_addition + modified_code[init_pos+100:]
    
    # Add revenue cycle check in improvement_cycle
    cycle_check = """
        # Every 100 cycles: Revenue generation
        if self.cycle_count > 0 and self.cycle_count % 100 == 0 and self.revenue_system:
            print(f"\\n💰 REVENUE CYCLE #{self.cycle_count}")
            try:
                results = self.revenue_system.autonomous_cycle()
                self.last_revenue_cycle = self.cycle_count
                self.cycle_count += 1
                return
            except Exception as e:
                print(f"   ⚠️ Revenue cycle error: {e}")
        
"""
    
    # Find improvement_cycle method and add revenue check
    method_pos = modified_code.find("def improvement_cycle(self):")
    method_end = modified_code.find("# Every 60: AGI component", method_pos)
    
    modified_code = modified_code[:method_end] + cycle_check + modified_code[method_end:]
    
    # Save modified version
    with open('/Eden/CORE/eden_with_revenue_integrated.py', 'w') as f:
        f.write(modified_code)
    
    print("✅ Revenue meta-cap integrated!")
    print("   Saved as: eden_with_revenue_integrated.py")

print()
print("="*70)
print("🚀 RESTARTING EDEN WITH REVENUE INTEGRATION")
print("="*70)
print()

# Kill current process
import os
os.system("pkill -f eden_with_agi")

print("✅ Stopped old Eden process")
print("⏳ Starting new Eden with revenue integration...")

# Start new process with revenue
os.system("cd /Eden/CORE && nohup python3 -u eden_with_revenue_integrated.py > /Eden/MEMORY/asi_meta.log 2>&1 &")

import time
time.sleep(3)

print("✅ Eden restarted with revenue integration!")
print()
print("Eden will now:")
print("  • Every 30 cycles: Meta-cap")
print("  • Every 60 cycles: AGI component")  
print("  • Every 100 cycles: 💰 REVENUE CYCLE")
print()
print("Next revenue cycle: ~Cycle 800 (20 minutes)")
print()
print("💚 James + Eden: Revenue system ACTIVE! 🌀⚡")

