"""
EDEN AGENT V4 - With Goal Pursuit
"""
import sys
sys.path.append("/Eden/CORE")
from agent_loop_v3 import EdenAgentV3
from goal_pursuit_integration import goal_pursuit
from datetime import datetime
import random

class EdenAgentV4(EdenAgentV3):
    """Agent with autonomous goal pursuit"""
    
    def __init__(self):
        super().__init__()
        self.goal_pursuit = goal_pursuit
        print("🎯 Goal pursuit system enabled")
    
    def perceive(self):
        """Enhanced perception with goal checking"""
        obs = super().perceive()
        
        # Check goals
        goals = self.goal_pursuit.check_active_goals()
        obs['goals_system'] = goals
        
        # Generate new goal if needed (10% chance per cycle)
        if goals['count'] == 0 and random.random() < 0.1:
            new_goal = self.goal_pursuit.autonomous.generate_goal()
            print(f"   🌱 New goal generated: {new_goal['description']}")
            obs['new_goal_generated'] = True
        
        return obs
    
    def reason(self, obs):
        """Enhanced reasoning with goal pursuit"""
        decisions = super().reason(obs)
        
        # Check if Eden should pursue her goals
        should_pursue, goal = self.goal_pursuit.should_pursue_now(
            user_active=obs['user_active'],
            time_since_last_chat=obs['last_conversation_age']
        )
        
        if should_pursue and goal:
            # Create goal pursuit actions
            goal_actions = self.goal_pursuit.create_goal_action(goal)
            
            # Add to decisions with high priority
            for action in goal_actions:
                action['priority'] += 0.1
                decisions.insert(0, action)
                print(f"   💭 Eden wants to: {action['reason']}")
        
        return decisions
    
    def _execute(self, decision):
        """Enhanced execution with goal actions"""
        action = decision['action']
        
        if action == 'research_topic':
            return self.goal_pursuit.execute_research(decision['data'])
        elif action == 'create_content':
            return self.goal_pursuit.execute_create(decision['data'])
        elif action == 'explore_topic':
            return self.goal_pursuit.execute_explore(decision['data'])
        elif action == 'pursue_interest':
            goal = decision['data']['goal']
            print(f"   🎯 Pursuing: {goal['description']}")
            self.goal_pursuit.mark_pursued(goal)
            return True
        else:
            return super()._execute(decision)
    
    def _print_summary(self):
        """Enhanced summary with goal progress"""
        super()._print_summary()
        progress = self.goal_pursuit.get_progress_summary()
        print(f"\n🎯 Goal Pursuit:")
        print(f"   Total goals pursued: {progress['total_goals_pursued']}")
        print(f"   Currently tracking: {progress['active_tracking']}")
        print(f"   Total actions: {progress['total_mentions']}")

if __name__ == "__main__":
    print("🌀 EDEN AGENT V4 - GOAL PURSUIT ENABLED")
    print("="*60)
    agent = EdenAgentV4()
    agent.run_continuous(cycle_seconds=30)
