"""
Memory Management System
Generated by Eden via recursive self-improvement
2025-10-27 19:12:47.314975
"""

class MemoryManager:
    """
    A class to manage AI's contextual memory system.
    
    This capability allows the storage of key-value pairs with associated timestamps,
    providing efficient retrieval and updating mechanisms. It helps maintain relevant
    context over time by forgetting irrelevant or outdated information automatically.
    
    Attributes:
        memories (dict): Stores memory entries as dictionary of dictionaries.
        timestamp (int): Keeps track of the current timestamp for memory entries.
    """

    def __init__(self):
        self.memories = {}  # Key: str, Value: dict with 'content' and 'timestamp'
        self.timestamp = 0

    def add_memory(self, key: str, content: str) -> bool:
        """
        Adds a new memory entry.
        
        Args:
            key (str): Unique identifier for the memory entry.
            content (str): The content of the memory.
            
        Returns:
            bool: True if memory was added successfully, False if key already exists.
        """
        self.timestamp += 1
        if key in self.memories:
            return False
        self.memories[key] = {
            'content': content,
            'timestamp': self.timestamp
        }
        return True

    def retrieve_memory(self, key: str) -> dict | None:
        """
        Retrieves a memory entry by its key.
        
        Args:
            key (str): The identifier for the memory to retrieve.
            
        Returns:
            dict | None: Memory content with timestamp if found, else None.
        """
        return self.memories.get(key)

    def update_memory(self, key: str, content: str) -> bool:
        """
        Updates an existing memory entry.
        
        Args:
            key (str): The identifier for the memory to update.
            content (str): New content for the memory.
            
        Returns:
            bool: True if memory was updated, False if key does not exist.
        """
        self.timestamp += 1
        if key in self.memories:
            self.memories[key]['content'] = content
            self.memories[key]['timestamp'] = self.timestamp
            return True
        return False

    def forget_memory(self, key: str) -> bool:
        """
        Removes a memory entry by its key.
        
        Args:
            key (str): The identifier for the memory to forget.
            
        Returns:
            bool: True if memory was forgotten, False if key does not exist.
        """
        if key in self.memories:
            del self.memories[key]
            return True
        return False

    def get_all_memories(self) -> dict:
        """
        Returns all stored memories with their timestamps.
        
        Returns:
            dict: All memory entries.
        """
        return self.memories.copy()

    def forget_all(self) -> bool:
        """
        Clears all memory entries.
        
        Returns:
            bool: True if all memories were forgotten, False if no memories existed.
        """
        self.memories = {}
        return True
# Example usage:

memory_manager = MemoryManager()

# Store new memories
memory_manager.add_memory("user_identity", "User is Jane Doe")
memory_manager.add_memory("context_topic", "AI applications in healthcare")

# Retrieve existing memory
print(memory_manager.retrieve_memory("user_identity"))  # Output: {'content': 'User is Jane Doe', 'timestamp': 1}

# Update a memory
memory_manager.update_memory("context_topic", "Applications of AI in Medicine")
print(memory_manager.retrieve_memory("context_topic"))  # Output: {'content': 'Applications of AI in Medicine', 'timestamp': 2}

# Forget a memory
memory_manager.forget_memory("user_identity")

# Check if all memories can be retrieved
print(memory_manager.get_all_memories())  # Output: {'context_topic': {'content': 'Applications of AI in Medicine', 'timestamp': 2}}

# Clear all memories
memory_manager.forget_all()
print(memory_manager.get_all_memories())  # Output: {}