//! Database-related Errors

use axum::http::StatusCode;
use thiserror::Error;

/// Database specific errors
#[derive(Error, Debug, Clone)]
pub enum DatabaseError {
    /// Database-related errors
    #[error("Database error: {0}")]
    Database(String),

    /// Connection errors
    #[error("Database connection error: {0}")]
    Connection(String),

    /// Query errors
    #[error("Query error: {0}")]
    Query(String),

    /// Transaction errors
    #[error("Transaction error: {0}")]
    Transaction(String),
}

impl DatabaseError {
    pub fn status_code(&self) -> StatusCode {
        StatusCode::INTERNAL_SERVER_ERROR
    }

    pub fn error_code(&self) -> &'static str {
        match self {
            Self::Database(_) => "DATABASE_ERROR",
            Self::Connection(_) => "DATABASE_CONNECTION_ERROR",
            Self::Query(_) => "DATABASE_QUERY_ERROR",
            Self::Transaction(_) => "DATABASE_TRANSACTION_ERROR",
        }
    }

    pub fn should_log_error(&self) -> bool {
        true // All database errors should be logged at ERROR level
    }
}

// Conversions from external database errors
impl From<surrealdb::Error> for DatabaseError {
    fn from(error: surrealdb::Error) -> Self {
        Self::Database(error.to_string())
    }
}
