"""Waterlight SDK error hierarchy."""

from __future__ import annotations


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

    def __init__(
        self,
        message: str,
        status_code: int | None = None,
        request_id: str | None = None,
    ):
        super().__init__(message)
        self.message = message
        self.status_code = status_code
        self.request_id = request_id


class AuthenticationError(WaterlightError):
    """Invalid or missing API key."""
    pass


class RateLimitError(WaterlightError):
    """Rate limit exceeded."""

    def __init__(
        self,
        message: str,
        status_code: int | None = None,
        request_id: str | None = None,
        retry_after: float | None = None,
    ):
        super().__init__(message, status_code=status_code, request_id=request_id)
        self.retry_after = retry_after


class InsufficientCreditsError(WaterlightError):
    """Account has insufficient credits."""
    pass


class APIError(WaterlightError):
    """Server-side error."""
    pass


def _map_status(status: int, message: str, request_id: str | None = None,
                retry_after: float | None = None) -> WaterlightError:
    """Map HTTP status to the appropriate error class."""
    if status == 401:
        return AuthenticationError(message, status_code=status, request_id=request_id)
    if status == 402:
        return InsufficientCreditsError(message, status_code=status, request_id=request_id)
    if status == 429:
        return RateLimitError(message, status_code=status, request_id=request_id,
                              retry_after=retry_after)
    return APIError(message, status_code=status, request_id=request_id)
