"""SDK-specific exceptions for the Daly Energy API client."""


class DalyAPIError(Exception):
    """Base exception for all Daly API errors.

    Attributes:
        status_code: HTTP status code from the API response.
        detail: Human-readable error message.
        response_body: Raw response body dict, if available.
    """

    def __init__(self, status_code: int, detail: str, response_body: dict | None = None):
        self.status_code = status_code
        self.detail = detail
        self.response_body = response_body
        super().__init__(f"[{status_code}] {detail}")


class AuthenticationError(DalyAPIError):
    """Raised when authentication fails (401)."""

    def __init__(self, detail: str = "Authentication failed", response_body: dict | None = None):
        super().__init__(401, detail, response_body)


class ForbiddenError(DalyAPIError):
    """Raised when the request is forbidden (403)."""

    def __init__(self, detail: str = "Forbidden", response_body: dict | None = None):
        super().__init__(403, detail, response_body)


class NotFoundError(DalyAPIError):
    """Raised when the requested resource is not found (404)."""

    def __init__(self, detail: str = "Resource not found", response_body: dict | None = None):
        super().__init__(404, detail, response_body)


class ConflictError(DalyAPIError):
    """Raised when the request conflicts with the current resource state (409).

    Typically indicates a concurrent modification or duplicate resource.
    """

    def __init__(self, detail: str = "Resource conflict", response_body: dict | None = None):
        super().__init__(409, detail, response_body)


class ValidationError(DalyAPIError):
    """Raised when the API rejects the request due to validation errors (400/422)."""

    def __init__(self, status_code: int, detail: str, response_body: dict | None = None):
        super().__init__(status_code, detail, response_body)


class RateLimitError(DalyAPIError):
    """Raised when the API rate limit is exceeded (429).

    Attributes:
        retry_after: Seconds to wait before retrying, if provided by the API.
    """

    def __init__(
        self,
        detail: str = "Rate limit exceeded",
        response_body: dict | None = None,
        retry_after: float | None = None,
    ):
        self.retry_after = retry_after
        super().__init__(429, detail, response_body)


class ServerError(DalyAPIError):
    """Raised when the API returns a server error (5xx)."""

    def __init__(self, status_code: int, detail: str = "Internal server error", response_body: dict | None = None):
        super().__init__(status_code, detail, response_body)


class TimeoutError(DalyAPIError):
    """Raised when a request to the API times out.

    This wraps httpx timeout exceptions into the SDK exception hierarchy
    so callers can catch ``DalyAPIError`` uniformly.
    """

    def __init__(self, detail: str = "Request timed out"):
        super().__init__(0, detail, None)
