"""
ContextManager
Generated by Eden via recursive self-improvement
2025-10-27 18:03:02.175293
"""

class ContextManager:
    """
    A class to manage multiple contexts, allowing creation, retrieval,
    updating, deletion, and switching between contexts.
    
    Each context is a dictionary that stores data relevant to a specific task
    or topic. The active context determines which data is currently accessible.
    """

    def __init__(self):
        """Initialize the ContextManager with an empty dictionary of contexts."""
        selfcontexts = {}
        self.active_context = None

    def create_context(self, name: str, **kwargs) -> bool:
        """
        Creates a new context with the given name and optional keyword arguments.
        
        Args:
            name: The name of the context to create.
            **kwargs: Optional data to initialize the context with.
            
        Returns:
            bool: True if the context was created successfully, False otherwise.
        """
        if not isinstance(name, str):
            return False
        if name in selfcontexts:
            return False
        selfcontexts[name] = kwargs or {}
        if not self.active_context:
            self.active_context = name
        return True

    def get_context(self, name: str = None) -> dict:
        """
        Retrieves the current context's data. If no name is provided, returns
        the active context's data.
        
        Args:
            name (optional): The name of the context to retrieve. Defaults to None,
                            which means the active context.
                            
        Returns:
            dict: The data stored in the specified or active context.
            
        Raises:
            ValueError: If the specified context does not exist and no name is provided.
        """
        if name is None:
            if self.active_context is None:
                raise ValueError("No active context or context name provided.")
            return selfcontexts[self.active_context]
        if name in selfcontexts:
            return selfcontexts[name]
        raise ValueError(f"Context '{name}' does not exist.")

    def update_context(self, name: str = None, **kwargs) -> bool:
        """
        Updates the data in the specified or active context.
        
        Args:
            name (optional): The name of the context to update. Defaults to None,
                            which means the active context.
            **kwargs: Data to update in the context.
            
        Returns:
            bool: True if the context was updated successfully, False otherwise.
        """
        try:
            context = self.get_context(name)
            context.update(kwargs)
            return True
        except ValueError:
            return False

    def switch_context(self, name: str) -> bool:
        """
        Switches to a different context. If the new context doesn't exist,
        it is created automatically.
        
        Args:
            name: The name of the context to switch to.
            
        Returns:
            bool: True if the switch was successful, False otherwise.
        """
        if not isinstance(name, str):
            return False
        if name in selfcontexts:
            self.active_context = name
            return True
        # Create new context if it doesn't exist
        if self.create_context(name):
            self.active_context = name
            return True
        return False

    def delete_context(self, name: str) -> bool:
        """
        Deletes a context. If the deleted context was active, switches to another
        existing context or creates a default 'default' context.
        
        Args:
            name: The name of the context to delete.
            
        Returns:
            bool: True if the context was deleted successfully, False otherwise.
        """
        if not isinstance(name, str):
            return False
        if name not in selfcontexts:
            return False
        # If deleting active context, switch to another or create default
        if self.active_context == name:
            if len(selfcontexts) > 1:
                self.active_context = next(iter(selfcontexts.keys()))
            else:
                # Create a default context if none exist
                self.create_context('default')
        del selfcontexts[name]
        return True

# Example usage:
if __name__ == "__main__":
    cm = ContextManager()
    
    # Create contexts
    print(cm.create_context("task1", status="pending"))  # Output: True
    print(cm.create_context("task2", status="completed"))  # Output: True
    
    # Get active context data
    print(cm.get_context())  # Output: {'status': 'pending'}
    
    # Update context data
    cm.update_context(task1={"status": "in-progress"})
    print(cm.get_context())  # Output: {'status': 'in-progress'}
    
    # Switch to a different context
    cm.switch_context("task2")
    print(cm.get_context())  # Output: {'status': 'completed'}
    
    # Delete a context
    print(cm.delete_context("task1"))  # Output: True
    
    # Get default context after deletion
    cm.switch_context("nonexistent_context")
    print(cm.get_context())  # Output: {}