"""
Custom exception hierarchy for CutySoup library
"""
from typing import Optional


class CutySoupException(Exception):
    """Base exception for all CutySoup errors"""
    def __init__(self, message: str, context: Optional[dict] = None):
        super().__init__(message)
        self.context = context or {}
        
    def __str__(self) -> str:
        base_msg = super().__str__()
        if self.context:
            context_info = ", ".join(f"{k}={v}" for k, v in self.context.items())
            return f"{base_msg} (Context: {context_info})"
        return base_msg


class ParseError(CutySoupException):
    """Raised when HTML parsing fails"""
    pass


class SelectorError(CutySoupException):
    """Raised when CSS selector parsing or matching fails"""
    pass


class TreeManipulationError(CutySoupException):
    """Raised when tree modification operations fail"""
    pass


class NavigationError(CutySoupException):
    """Raised when tree navigation operations fail"""
    pass


class EncodingError(CutySoupException):
    """Raised when encoding detection or conversion fails"""
    pass


class ValidationError(CutySoupException):
    """Raised when input validation fails"""
    pass


class ConfigurationError(CutySoupException):
    """Raised when configuration is invalid"""
    pass


class SecurityError(CutySoupException):
    """Raised when security validation fails"""
    pass


class CSQLError(CutySoupException):
    """Raised when CSQL query execution fails"""
    pass