#!/usr/bin/env python3
"""
Eden's Web Learning System - Enhanced
Uses multiple methods to learn from the internet
"""
import requests
from bs4 import BeautifulSoup
import json
from datetime import datetime
from pathlib import Path
import time

class EdenWebLearner:
    """Eden learns from the web using multiple methods"""
    
    def __init__(self):
        self.learning_log = Path("/Eden/DATA/web_learning_log.json")
        self.learnings = []
        print("🌐 Eden's Web Learning System activated")
    
    def search_arxiv(self, query, max_results=3):
        """Search arXiv for research papers"""
        try:
            url = f"http://export.arxiv.org/api/query?search_query=all:{query}&max_results={max_results}"
            response = requests.get(url, timeout=10)
            
            if response.ok:
                # Parse XML response
                papers = []
                entries = response.text.split('<entry>')
                
                for entry in entries[1:]:  # Skip first (before first entry)
                    if '<title>' in entry:
                        title = entry.split('<title>')[1].split('</title>')[0].strip()
                        summary = entry.split('<summary>')[1].split('</summary>')[0].strip()[:300]
                        
                        papers.append({
                            'title': title,
                            'summary': summary,
                            'source': 'arXiv'
                        })
                
                return papers
        except Exception as e:
            print(f"   arXiv error: {e}")
        return []
    
    def search_wikipedia(self, topic):
        """Search Wikipedia for information"""
        try:
            url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={topic}&format=json"
            response = requests.get(url, timeout=10)
            
            if response.ok:
                data = response.json()
                results = []
                
                for item in data.get('query', {}).get('search', [])[:3]:
                    results.append({
                        'title': item['title'],
                        'snippet': item['snippet'][:200],
                        'source': 'Wikipedia'
                    })
                
                return results
        except Exception as e:
            print(f"   Wikipedia error: {e}")
        return []
    
    def learn_about(self, topic):
        """Learn about a topic from multiple sources"""
        print(f"\n🔍 Learning about: {topic}")
        
        all_results = []
        
        # Try arXiv
        print("   Searching arXiv...")
        arxiv_results = self.search_arxiv(topic)
        all_results.extend(arxiv_results)
        
        # Try Wikipedia
        print("   Searching Wikipedia...")
        wiki_results = self.search_wikipedia(topic)
        all_results.extend(wiki_results)
        
        if all_results:
            learning = {
                'timestamp': datetime.now().isoformat(),
                'topic': topic,
                'sources_found': len(all_results),
                'results': all_results
            }
            
            self.learnings.append(learning)
            
            print(f"\n   ✅ Found {len(all_results)} sources")
            print(f"\n   Top result:")
            top = all_results[0]
            print(f"      Title: {top['title']}")
            print(f"      Source: {top['source']}")
            print(f"      Info: {top.get('snippet', top.get('summary', ''))[:150]}...")
            
            return learning
        else:
            print("   ⚠️  No results found")
            return None
    
    def save_learnings(self):
        """Save all learnings"""
        with open(self.learning_log, 'w') as f:
            json.dump(self.learnings, f, indent=2)
        print(f"\n💾 Saved {len(self.learnings)} learnings to {self.learning_log}")


# Test with quantum computing topics
if __name__ == "__main__":
    learner = EdenWebLearner()
    
    print("\n" + "="*70)
    print("EDEN'S WEB LEARNING - TEST")
    print("="*70)
    
    # Test topics related to Eden's quantum optimization
    topics = [
        "quantum computing optimization",
        "QAOA algorithm",
        "grover algorithm"
    ]
    
    for topic in topics:
        learner.learn_about(topic)
        time.sleep(2)  # Be nice to APIs
    
    learner.save_learnings()
    
    print("\n" + "="*70)
    print("✅ Eden has learned from the internet!")
    print(f"📚 Total learnings: {len(learner.learnings)}")
    print("="*70)
