// Error types for VAPORA v1.0
// Phase 1: Comprehensive error handling with proper conversions

use thiserror::Error;

/// Main error type for VAPORA
#[derive(Error, Debug)]
pub enum VaporaError {
    /// Configuration loading or validation error
    #[error("Configuration error: {0}")]
    ConfigError(String),

    /// Database operation error
    #[error("Database error: {0}")]
    DatabaseError(String),

    /// Resource not found error
    #[error("Not found: {0}")]
    NotFound(String),

    /// Invalid input or validation error
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Authentication or authorization error
    #[error("Unauthorized: {0}")]
    Unauthorized(String),

    /// Agent system error
    #[error("Agent error: {0}")]
    AgentError(String),

    /// LLM router error
    #[error("LLM router error: {0}")]
    LLMRouterError(String),

    /// Workflow execution error
    #[error("Workflow error: {0}")]
    WorkflowError(String),

    /// NATS messaging error
    #[error("NATS error: {0}")]
    NatsError(String),

    /// IO operation error
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    /// Serialization/deserialization error
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),

    /// TOML parsing error
    #[error("TOML error: {0}")]
    TomlError(String),

    /// Internal server error
    #[error("Internal server error: {0}")]
    InternalError(String),
}

/// Result type alias using VaporaError
pub type Result<T> = std::result::Result<T, VaporaError>;

// ============================================================================
// Error Conversions
// ============================================================================

#[cfg(feature = "backend")]
impl From<surrealdb::Error> for VaporaError {
    fn from(err: surrealdb::Error) -> Self {
        VaporaError::DatabaseError(err.to_string())
    }
}

impl From<toml::de::Error> for VaporaError {
    fn from(err: toml::de::Error) -> Self {
        VaporaError::TomlError(err.to_string())
    }
}
