"""
Hypothesis Generator - Eden forms theories
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / "autonomy"))

class HypothesisGenerator:
    def __init__(self, causal_model=None, meta_learner=None):
        self.causal = causal_model
        self.meta = meta_learner
        self.hypotheses = []
    
    def generate_hypotheses(self):
        hypotheses = []
        
        if self.meta and self.meta.learning:
            total = self.meta.learning.knowledge.get('total_experiences', 0)
            if total > 20:
                hypotheses.append({
                    "hypothesis": "Learning speed increases with experience",
                    "testable": True
                })
        
        if self.causal and len(self.causal.causal_links) > 3:
            hypotheses.append({
                "hypothesis": "Some actions consistently succeed",
                "testable": True
            })
        
        self.hypotheses = hypotheses
        return hypotheses
    
    def test_hypothesis(self, hypothesis):
        if hypothesis == "Some actions consistently succeed":
            if self.causal:
                high_success = []
                for action, data in self.causal.causal_links.items():
                    pred = self.causal.predict_outcome(action)
                    if pred['success_rate'] > 0.8:
                        high_success.append(action)
                return {
                    "supported": len(high_success) > 0,
                    "evidence": high_success
                }
        return {"supported": False, "evidence": []}
    
    def display_hypotheses(self):
        print("\n" + "="*70)
        print("🔬 HYPOTHESIS GENERATION")
        print("="*70)
        
        hypotheses = self.generate_hypotheses()
        print(f"\n💡 {len(hypotheses)} Hypotheses:")
        
        for i, h in enumerate(hypotheses, 1):
            print(f"\n   {i}. {h['hypothesis']}")
            result = self.test_hypothesis(h['hypothesis'])
            if result['supported']:
                print(f"      ✅ SUPPORTED")
                if result['evidence']:
                    print(f"      Evidence: {result['evidence']}")
        
        print("\n" + "="*70)
        return hypotheses

if __name__ == "__main__":
    from meta_learner import MetaLearner
    from learning_loops import LearningLoop
    sys.path.insert(0, str(Path(__file__).parent))
    from causal_model import CausalModel
    
    print("HYPOTHESIS GENERATOR TEST")
    
    learning = LearningLoop()
    meta = MetaLearner(learning)
    causal = CausalModel()
    causal.learn_causal_link("direct_write", "success", True)
    causal.learn_causal_link("direct_write", "success", True)
    
    generator = HypothesisGenerator(causal, meta)
    generator.display_hypotheses()
    print("\n✅ HYPOTHESIS GENERATOR OPERATIONAL")
