"""
Neuralk exception module.

This module defines the custom exception used throughout the Neuralk AI SDK.
"""

from http import HTTPStatus

import requests


class NeuralkException(RuntimeError):
    """
    Custom exception for errors raised by the Neuralk AI SDK.

    Attributes:
        message (str): The error message.
        status_code (HTTPStatus): The HTTP status code associated with the error.
        details (str): Additional details about the error.
    """

    def __init__(self, message: str, status_code: HTTPStatus, details: str):
        """
        Initialize a NeuralkException instance.

        Args:
            message (str): The error message.
            status_code (HTTPStatus): The HTTP status code associated with the error.
            details (str): Additional details about the error.
        """
        super().__init__(message, status_code, details)
        self.message = message
        self.status_code = status_code
        self.details = details

    @staticmethod
    def from_resp(msg: str, resp: requests.Response) -> "NeuralkException":
        """
        Create a NeuralkException from an HTTP response.

        Args:
            msg (str): The error message.
            resp (requests.Response): The HTTP response object.

        Returns:
            NeuralkException: The constructed exception instance.
        """
        return NeuralkException(msg, resp.status_code, resp.text)


class NeuralkTermsNotAcceptedError(NeuralkException):
    """
    Raised when the organization has not accepted the Terms of Service.

    The Neuralk API returns HTTP 403 with error code ``4030110``
    when the caller's organization has not yet accepted the current
    Terms of Service.

    Attributes:
        terms_version: The current Terms version that must be accepted.
        terms_url: URL to the full Terms of Service text.
    """

    def __init__(
        self,
        message: str,
        status_code: HTTPStatus,
        details: str,
        terms_version: str | None = None,
        terms_url: str | None = None,
    ):
        """
        Initialize a NeuralkTermsNotAcceptedError instance.

        Args:
            message: The error message.
            status_code: The HTTP status code associated with the error.
            details: Additional details about the error.
            terms_version: The current Terms version that must be accepted.
            terms_url: URL to the full Terms of Service text.
        """
        super().__init__(message, status_code, details)
        self.terms_version = terms_version
        self.terms_url = terms_url
