#!/usr/bin/env python3
"""
EDEN EVOLUTION DAEMON
Runs continuously, evolving capabilities 24/7
"""
import sys
import time
import sqlite3
from datetime import datetime

sys.path.insert(0, '/Eden/CORE')
from guided_evolution import TASKS, ADVANCED_TASKS, evolve_solution, DB, PHI

ALL_TASKS = TASKS + ADVANCED_TASKS

def log(msg):
    print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")

def run_cycle():
    """One evolution cycle through all tasks"""
    solved = 0
    total = len(ALL_TASKS)
    
    for task in ALL_TASKS:
        code, score = evolve_solution(task, generations=5)
        if score == 100:
            solved += 1
            # Save with timestamp
            conn = sqlite3.connect(DB)
            cap_id = f"evolved_{task['name']}_{int(time.time())}"
            conn.execute(
                "INSERT OR REPLACE INTO caps VALUES (?,?,?,?)",
                (cap_id, code, score * PHI * 30, int(time.time()))
            )
            conn.commit()
            conn.close()
    
    return solved, total

def main():
    log("🧬 EDEN EVOLUTION DAEMON STARTING")
    log("="*50)
    
    cycle = 0
    total_solved = 0
    
    while True:
        cycle += 1
        log(f"CYCLE {cycle}")
        
        solved, total = run_cycle()
        total_solved += solved
        
        log(f"  Solved: {solved}/{total} | Total evolved: {total_solved}")
        
        # Brief pause between cycles
        time.sleep(10)

if __name__ == "__main__":
    main()
