/// Error that can occur.
///
/// This enum encapsulates various types of errors, including authentication failures,
/// HTTP request errors, URL parsing errors, and JSON serialization/deserialization errors.
#[derive(Debug)]
pub enum Error {
    /// Represents a generic error that occurs during authentication.
    AuthFailed(String),
    /// Error that occurs when the response from a service is invalid or unexpected.
    InvalidResponse(String),
    /// Error that occurs when a request is invalid.
    InvalidRequest(String),
    /// Error that occurs during an HTTP request using the Reqwest library.
    ReqwestError(reqwest::Error),
    /// Error that occurs when parsing a URL fails.
    UrlParseError(url::ParseError),
    /// Error that occurs during JSON serialization or deserialization.
    SerdeJsonError(serde_json::Error),
    /// HTTP 409 Conflict error.
    Http409(String),
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Self::ReqwestError(err)
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Self::SerdeJsonError(err)
    }
}

impl From<url::ParseError> for Error {
    fn from(err: url::ParseError) -> Self {
        Self::UrlParseError(err)
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::AuthFailed(e) => e.to_string(),
                Self::InvalidResponse(e) => e.to_string(),
                Self::InvalidRequest(e) => e.to_string(),
                Self::ReqwestError(e) => e.to_string(),
                Self::UrlParseError(e) => e.to_string(),
                Self::SerdeJsonError(e) => e.to_string(),
                Self::Http409(e) => e.to_string(),
            }
        )
    }
}

impl std::error::Error for Error {}
