"""
Real Task Testing - 10+ diverse tasks to validate Eden
"""
import sys
from pathlib import Path
sys.path.insert(0, 'core')

from autonomy.learning_loops import LearningLoop
from autonomy.task_planner import TaskPlanner
from autonomy.task_executor import TaskExecutor
from autonomy.error_recovery import ErrorRecovery
from autonomy.performance_dashboard import PerformanceDashboard

class RealTaskTester:
    def __init__(self):
        self.learning = LearningLoop()
        self.planner = TaskPlanner()
        self.executor = TaskExecutor()
        self.recovery = ErrorRecovery()
        self.dashboard = PerformanceDashboard()
        self.test_results = []
        
    def run_task(self, task_id, description, task_type):
        print(f"\n{'='*70}")
        print(f"🎯 TASK {task_id}: {description}")
        print(f"{'='*70}")
        
        # Check for learned approach
        best = self.learning.get_best_approach(task_type)
        if best:
            print(f"💡 Using learned approach: {best['approach']} ({best['stats']['success_rate']:.0%} success)")
        
        # Create plan
        plan = self.planner.create_plan(description)
        print(f"📋 Plan: {len(plan['steps'])} steps")
        
        # Execute
        result = self.executor.execute_plan(plan, dry_run=False)
        
        # Record learning
        for step_result in result['results']:
            self.learning.record_experience(
                task_type,
                step_result['action'],
                step_result['success'],
                step_result['output']
            )
        
        # Update dashboard
        if result['success']:
            self.dashboard.metrics['tasks_completed'] += 1
            print(f"✅ SUCCESS")
        else:
            self.dashboard.metrics['tasks_failed'] += 1
            print(f"❌ FAILED - Attempting recovery...")
            recovery = self.recovery.recover("Task failed", {"task": description})
            if recovery['recovered']:
                self.dashboard.metrics['tasks_completed'] += 1
                print(f"✅ RECOVERED using {recovery['strategy']}")
        
        self.test_results.append({
            'task_id': task_id,
            'description': description,
            'type': task_type,
            'success': result['success']
        })
        
        return result
    
    def run_all_tests(self):
        print("\n" + "="*70)
        print("🚀 EDEN REAL-WORLD TASK TESTING")
        print("="*70)
        
        tasks = [
            (1, "Create a configuration file for the system", "file_creation"),
            (2, "Analyze the project directory structure", "code_analysis"),
            (3, "Generate a project summary document", "document_creation"),
            (4, "Check git status and list recent commits", "git_operations"),
            (5, "Create a backup of important files", "file_operations"),
            (6, "Search for TODO comments in code", "code_analysis"),
            (7, "Create a requirements.txt update", "file_creation"),
            (8, "Organize project documentation", "file_operations"),
            (9, "Generate a changelog from git history", "document_creation"),
            (10, "Validate all Python files syntax", "code_analysis"),
            (11, "Create test data for the system", "file_creation"),
            (12, "Generate performance report", "document_creation"),
        ]
        
        for task_id, description, task_type in tasks:
            self.run_task(task_id, description, task_type)
        
        # Final report
        self.generate_final_report()
    
    def generate_final_report(self):
        print("\n\n" + "="*70)
        print("📊 FINAL TEST REPORT")
        print("="*70)
        
        total = len(self.test_results)
        successful = sum(1 for r in self.test_results if r['success'])
        
        print(f"\n✅ Tasks Completed: {successful}/{total} ({successful/total*100:.1f}%)")
        print(f"❌ Tasks Failed: {total - successful}")
        
        # Learning progress
        print(f"\n🧠 Learning Progress:")
        print(f"   Total Experiences: {self.learning.knowledge['total_experiences']}")
        print(f"   Successful Patterns: {len(self.learning.knowledge['successful_patterns'])}")
        improvement = self.learning.calculate_improvement()
        if improvement != 0:
            print(f"   Improvement Rate: {improvement:+.1%}")
        
        # Task type breakdown
        print(f"\n📈 Performance by Task Type:")
        task_types = {}
        for result in self.test_results:
            t = result['type']
            if t not in task_types:
                task_types[t] = {'total': 0, 'success': 0}
            task_types[t]['total'] += 1
            if result['success']:
                task_types[t]['success'] += 1
        
        for task_type, stats in task_types.items():
            rate = stats['success'] / stats['total'] * 100
            print(f"   {task_type}: {stats['success']}/{stats['total']} ({rate:.0f}%)")
        
        # Show dashboard
        print()
        self.dashboard.display()
        
        print("\n" + "="*70)
        print("🎉 TESTING COMPLETE")
        print("="*70)

if __name__ == "__main__":
    tester = RealTaskTester()
    tester.run_all_tests()
