use crate::DbElement;
use crate::DbError;
use crate::DbId;
use crate::DbImpl;
use crate::Query;
use crate::QueryResult;
use crate::StorageData;

/// Query to select all indexes in the database.
///
/// The result will be number of returned indexes and single element
/// with index 0 and the properties corresponding to the names of the indexes
/// (keys) with `u64` values representing number of indexed values in each
/// index.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "derive", derive(agdb::DbSerialize))]
#[cfg_attr(feature = "api", derive(agdb::TypeDef))]
#[derive(Clone, Debug, PartialEq)]
pub struct SelectIndexesQuery {}

impl Query for SelectIndexesQuery {
    fn process<Store: StorageData>(&self, db: &DbImpl<Store>) -> Result<QueryResult, DbError> {
        let mut result = QueryResult::default();
        let indexes = db.indexes();
        result.result = indexes.len() as u64;

        result.elements.push(DbElement {
            id: DbId::default(),
            from: DbId::default(),
            to: DbId::default(),
            values: indexes,
        });

        Ok(result)
    }
}

impl Query for &SelectIndexesQuery {
    fn process<Store: StorageData>(&self, db: &DbImpl<Store>) -> Result<QueryResult, DbError> {
        (*self).process(db)
    }
}
