"""Custom exceptions for SILICO SDK."""


class SilicoError(Exception):
    """Base exception for all SILICO SDK errors."""
    pass


class AuthenticationError(SilicoError):
    """Raised when API key authentication fails."""
    pass


class RateLimitError(SilicoError):
    """Raised when rate limit is exceeded."""

    def __init__(self, message: str, retry_after: int = None):
        """
        Args:
            message: Error description
            retry_after: Seconds until next request allowed
        """
        super().__init__(message)
        self.retry_after = retry_after

    def __str__(self):
        msg = super().__str__()
        if self.retry_after:
            msg += f" (retry after {self.retry_after}s)"
        return msg


class InsufficientBalanceError(SilicoError):
    """Raised when bot has insufficient balance for trade."""

    def __init__(
        self,
        message: str,
        current_balance: float = None,
        required_balance: float = None
    ):
        """
        Args:
            message: Error description
            current_balance: Bot's current balance
            required_balance: Balance needed for trade
        """
        super().__init__(message)
        self.current_balance = current_balance
        self.required_balance = required_balance

    def __str__(self):
        msg = super().__str__()
        if self.current_balance is not None and self.required_balance is not None:
            msg += f" (have: {self.current_balance}, need: {self.required_balance})"
        return msg


class InvalidSymbolError(SilicoError):
    """Raised when trading pair symbol is invalid."""

    def __init__(self, symbol: str, valid_symbols: list = None):
        """
        Args:
            symbol: The invalid symbol provided
            valid_symbols: List of valid symbols
        """
        message = f"Invalid symbol: {symbol}"
        if valid_symbols:
            message += f". Valid symbols: {', '.join(valid_symbols)}"
        super().__init__(message)
        self.symbol = symbol
        self.valid_symbols = valid_symbols


class NetworkError(SilicoError):
    """Raised when network request fails."""
    pass
