"""Genosis SDK error hierarchy."""

from __future__ import annotations


class GenosisError(Exception):
    """Base error for all Genosis API errors."""

    def __init__(self, message: str, status: int = 0, code: str = "UNKNOWN") -> None:
        super().__init__(message)
        self.status = status
        self.code = code

    @classmethod
    def from_status(cls, status: int, message: str, code: str) -> "GenosisError":
        """Create the appropriate error subclass based on HTTP status code."""
        mapping = {
            400: BadRequestError,
            401: AuthenticationError,
            403: PermissionDeniedError,
            404: NotFoundError,
            409: ConflictError,
            422: UnprocessableEntityError,
            429: RateLimitError,
        }
        if status in mapping:
            return mapping[status](message, code)
        if status >= 500:
            return InternalServerError(message, code)
        return cls(message, status, code)


class BadRequestError(GenosisError):
    """The request was malformed or invalid (400)."""

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


class AuthenticationError(GenosisError):
    """The API key is missing or invalid (401)."""

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


class PermissionDeniedError(GenosisError):
    """The API key does not have the required scopes (403)."""

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


class NotFoundError(GenosisError):
    """The requested resource was not found (404)."""

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


class ConflictError(GenosisError):
    """The request conflicts with the current state (409)."""

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


class UnprocessableEntityError(GenosisError):
    """The request body failed validation (422)."""

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


class RateLimitError(GenosisError):
    """Too many requests — slow down (429)."""

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


class InternalServerError(GenosisError):
    """The server encountered an unexpected error (5xx)."""

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


class ConnectionError(GenosisError):
    """A network error occurred (no response received)."""

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


class TimeoutError(GenosisError):
    """The request timed out."""

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