//! [`tarpc`] service definition for Omry.
use omry_archiving::{Record, RecordParams};
use omry_common::RecordId;
use omry_search::SearchResponse;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::search_wrappers::SearchParametersWrapper;

/// Status returned by the service.
#[allow(missing_docs)]
#[derive(Debug, Serialize, Deserialize)]
pub enum ResponseStatus {
    Ok,
    Error,
}

/// flora RPC service error.
pub type FloraError = String;

/// Result alias for fallible operations with flora RPC service.
pub type OmryResult<T> = Result<T, FloraError>;

/// Type alias for a collection of record IDs.
pub type RecordIds = Vec<RecordId>;

/// Expected message on successful ping.
pub const PONG: &str = "pong 🌱";

/// Semantic version of the Omry service.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Omry service RPCs.
#[allow(missing_docs)]
#[tarpc::service]
pub trait FloraService {
    /// Pings the server. Can be used to check the server is alive.
    ///
    /// A server is expected to respond with [`PONG`].
    async fn ping() -> String;

    /// Adds a record to flora.
    async fn add_record(record_params: RecordParams) -> OmryResult<RecordId>;

    /// Adds multiple records to flora.
    async fn add_many_records(records: Vec<RecordParams>) -> OmryResult<RecordIds>;

    /// Returns the count of records in the database.
    async fn count() -> OmryResult<i64>;

    /// Gets records matching the given IDs.
    async fn get_records(record_ids: RecordIds) -> OmryResult<Arc<Vec<Record>>>;

    /// Gets all records in the Omry database.
    async fn get_all_records() -> OmryResult<Arc<Vec<Record>>>;

    /// Deletes record with the given ID.
    async fn delete_record(record_id: RecordId) -> OmryResult<usize>;

    /// Searches Omry's Typesense instance.
    async fn search(search_params: SearchParametersWrapper) -> OmryResult<SearchResponse>;

    /// Returns the version of the Omry service.
    async fn version() -> OmryResult<String>;
}
