use super::vitur_client;
use crate::zerokms::vitur_client::connection::ConnectionInitError;
use miette::Diagnostic;
use thiserror::Error;

#[derive(Error, Debug, Diagnostic)]
pub enum Error {
    #[error(transparent)]
    #[diagnostic(transparent)]
    CreateKeyset(#[from] vitur_client::CreateKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    ListKeyset(#[from] vitur_client::ListKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    EnableKeyset(#[from] vitur_client::EnableKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    ModifyKeyset(#[from] vitur_client::ModifyKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    DisableKeyset(#[from] vitur_client::DisableKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    CreateClient(#[from] vitur_client::CreateClientError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    ListClient(#[from] vitur_client::ListClientError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    RevokeKeyset(#[from] vitur_client::RevokeKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    RevokeClient(#[from] vitur_client::RevokeClientError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    GrantKeyset(#[from] vitur_client::GrantKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    SaveConfig(#[from] vitur_client::SaveConfigError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    LoadConfig(#[from] vitur_client::LoadConfigError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    Decrypt(#[from] vitur_client::DecryptError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    Encrypt(#[from] vitur_client::EncryptError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    Auth(#[from] stack_auth::AuthError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    GenerateKey(#[from] vitur_client::GenerateKeyError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    LoadKeyset(#[from] vitur_client::LoadKeysetError),

    #[error(transparent)]
    #[diagnostic(transparent)]
    ConfigError(#[from] crate::config::errors::ConfigError),

    #[error(transparent)]
    ConnectionInit(#[from] ConnectionInitError),

    #[error("Unexpected error: {0}")]
    Unexpected(String),
}

impl Error {
    pub fn is_conflict(&self) -> bool {
        match self {
            Error::CreateKeyset(e) => e.is_conflict(),
            _ => false,
        }
    }
}
