"""Typed exception hierarchy for the analytics SDK."""

from __future__ import annotations


class AnalyticsError(Exception):
    """Base exception for all SDK errors."""

    def __init__(self, message: str, code: str) -> None:
        super().__init__(message)
        self.code = code


class AuthenticationError(AnalyticsError):
    """Raised when the API key is invalid."""

    def __init__(self) -> None:
        super().__init__("Invalid API key", "AUTHENTICATION_ERROR")


class ValidationError(AnalyticsError):
    """Raised when event data fails validation."""

    def __init__(self, message: str) -> None:
        super().__init__(message, "VALIDATION_ERROR")


class NetworkError(AnalyticsError):
    """Raised on transport / HTTP errors."""

    def __init__(self, message: str, status_code: int | None = None) -> None:
        super().__init__(message, "NETWORK_ERROR")
        self.status_code = status_code


class RateLimitError(AnalyticsError):
    """Raised when the server responds with 429."""

    def __init__(self, retry_after_ms: int) -> None:
        super().__init__(
            f"Rate limited. Retry after {retry_after_ms}ms",
            "RATE_LIMIT_ERROR",
        )
        self.retry_after_ms = retry_after_ms


class TimeoutError(AnalyticsError):
    """Raised when a request exceeds the configured timeout."""

    def __init__(self, timeout_seconds: float) -> None:
        super().__init__(
            f"Request timed out after {timeout_seconds}s",
            "TIMEOUT_ERROR",
        )
