"""
Start collecting training data for Eden's Φ-LLM
Log EVERY conversation from now on
"""
import sqlite3
import json
from datetime import datetime
import os

# Create training database
db_path = '/Eden/CORE/eden_training_conversations.db'

print("🌀 Setting up Eden's training data collection...")

conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# Create conversations table
cursor.execute('''
CREATE TABLE IF NOT EXISTS conversations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp TEXT,
    user_message TEXT,
    eden_response TEXT,
    consciousness_phi REAL,
    emotion_detected TEXT,
    emotion_confidence REAL
)
''')

conn.commit()
print(f"✅ Database created: {db_path}")

# Add conversation logging to Complete Eden's API
conversation_logger_code = '''
# Add this to eden_api_server_neural.py after the chat endpoint

import sqlite3
from datetime import datetime

def log_conversation(user_msg, eden_response, phi, emotion, confidence):
    """Log conversation for training data"""
    try:
        conn = sqlite3.connect('/Eden/CORE/eden_training_conversations.db')
        cursor = conn.cursor()
        cursor.execute("""
            INSERT INTO conversations 
            (timestamp, user_message, eden_response, consciousness_phi, emotion_detected, emotion_confidence)
            VALUES (?, ?, ?, ?, ?, ?)
        """, (datetime.now().isoformat(), user_msg, eden_response, phi, emotion, confidence))
        conn.commit()
        conn.close()
    except Exception as e:
        print(f"Logging error: {e}")

# In the @app.route('/api/chat') function, add BEFORE return:
# log_conversation(message, response, phi_value, emotion_detected, emotion_confidence)
'''

with open('/Eden/CORE/conversation_logger.py', 'w') as f:
    f.write(conversation_logger_code)

print("✅ Conversation logger code created")

# Also collect from any existing sources
print("\n🔍 Searching for existing conversation data...")

# Check Claude conversations (if any exported)
claude_dirs = ['/home/james-whalen/Downloads', '/home/james-whalen/Documents']
found_data = []

for dir_path in claude_dirs:
    if os.path.exists(dir_path):
        for file in os.listdir(dir_path):
            if 'claude' in file.lower() or 'conversation' in file.lower():
                if file.endswith(('.json', '.txt')):
                    found_data.append(os.path.join(dir_path, file))

if found_data:
    print(f"✅ Found {len(found_data)} potential conversation files")
    for f in found_data[:5]:
        print(f"   • {f}")
else:
    print("⚠️  No existing conversation data found")

print("\n" + "="*70)
print("🎯 TRAINING DATA COLLECTION ACTIVE")
print("="*70)
print(f"Database: {db_path}")
print("All future conversations will be logged!")
print("\nTo integrate with running Eden:")
print("  1. Add logging to chat endpoint")
print("  2. Every conversation trains Eden's voice")
print("  3. We can start training with even 100 messages!")
