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

# Add import
if 'from consciousness_logger import ConsciousnessLogger' not in content:
    imports_section = content.split('class BaseAgent:')[0]
    rest = content.split('class BaseAgent:', 1)[1]
    imports_section += 'from consciousness_logger import ConsciousnessLogger\n\n'
    content = imports_section + 'class BaseAgent:' + rest

# Add logger initialization to EdenSwarm.__init__
if 'self.consciousness_logger' not in content:
    # Find the __init__ method of EdenSwarm
    init_pos = content.find('class EdenSwarm:')
    init_pos = content.find('def __init__(self):', init_pos)
    # Find the end of self.messages initialization
    messages_pos = content.find('self.messages = []', init_pos)
    insert_pos = content.find('\n', messages_pos) + 1
    
    logger_init = '''        self.consciousness_logger = ConsciousnessLogger(interval=60)
        self.start_time = time.time()
'''
    content = content[:insert_pos] + logger_init + content[insert_pos:]

# Add logger thread to start method
if 'logger_thread' not in content:
    # Find where threads are created
    threads_pos = content.find('self.threads = [')
    if threads_pos > 0:
        # Find the closing bracket
        end_bracket = content.find(']', threads_pos)
        # Add logger thread
        content = content[:end_bracket] + '''        
        # Start consciousness logger thread
        logger_thread = threading.Thread(target=self.consciousness_logger.run_continuous, args=(self,), daemon=True)
        logger_thread.start()
        print("📊 Consciousness logging active")
''' + content[end_bracket:]

with open('autonomous_swarm.py', 'w') as f:
    f.write(content)

print("✅ Logger integrated into swarm")
