/// Result type alias for arrDb operations.
pub type Result<T> = std::result::Result<T, ArrDbError>;

/// Errors produced by the arrDb system.
#[derive(Debug, thiserror::Error)]
pub enum ArrDbError {
    /// Type mismatch or invalid type operation.
    #[error("type error: {0}")]
    Type(String),

    /// Dimension-related error (out of range, wrong count, etc.).
    #[error("dimension error: {0}")]
    Dimension(String),

    /// Schema validation error.
    #[error("schema error: {0}")]
    Schema(String),

    /// Chunk addressing or format error.
    #[error("chunk error: {0}")]
    Chunk(String),

    /// Storage I/O error.
    #[error("storage error: {0}")]
    Storage(String),

    /// Query parsing or planning error.
    #[error("query error: {0}")]
    Query(String),

    /// Expression evaluation error.
    #[error("expression error: {0}")]
    Expression(String),

    /// Execution engine error.
    #[error("execution error: {0}")]
    Execution(String),

    /// Array not found in registry.
    #[error("array not found: '{0}'")]
    ArrayNotFound(String),

    /// Dataset not found in registry.
    #[error("dataset not found: '{0}'")]
    DatasetNotFound(String),

    /// Feature not yet implemented.
    #[error("not implemented: {0}")]
    NotImplemented(String),

    /// Underlying I/O error.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn error_display() {
        let e = ArrDbError::Type("expected Float32, got Int32".into());
        assert_eq!(e.to_string(), "type error: expected Float32, got Int32");
    }

    #[test]
    fn error_from_io() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file missing");
        let e: ArrDbError = io_err.into();
        assert!(matches!(e, ArrDbError::Io(_)));
    }

    #[test]
    fn result_type_works() {
        fn fallible() -> Result<()> {
            Err(ArrDbError::ArrayNotFound("test".into()))
        }
        assert!(fallible().is_err());
    }
}
