//! Message search delegated to [`crate::repo::traits::ChatRepository`].

use std::sync::Arc;

use sqlx::PgPool;

use crate::dto::commands::SearchMessages;
use crate::dto::results::SearchPage;
use crate::error::ChatResult;
use crate::repo::traits::ChatRepository;

/// Search facade used by [`crate::SqlxChatApp`].
#[derive(Clone)]
pub struct SearchService {
    repo: Arc<dyn ChatRepository>,
}

impl SearchService {
    /// Constructs a service backed by the given repository.
    pub fn new(repo: Arc<dyn ChatRepository>) -> Self {
        Self { repo }
    }

    /// Searches message bodies in rooms accessible to `actor`.
    pub async fn search_messages(
        &self,
        pool: &PgPool,
        organization_id: &str,
        actor: &str,
        input: SearchMessages,
    ) -> ChatResult<SearchPage> {
        self.repo
            .search_messages(pool, organization_id, actor, input)
            .await
    }
}
