//! Read-receipt cursor updates delegated to [`crate::repo::traits::ChatRepository`].

use std::sync::Arc;

use sqlx::PgPool;

use crate::error::ChatResult;
use crate::ids::{MessageId, RoomId};
use crate::repo::traits::{ChatRepository, ReadCursorCommit};

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

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

    /// Advances the actor's read cursor up to `seq` or `message_id` and returns an outbox commit.
    pub async fn mark_read_up_to(
        &self,
        pool: &PgPool,
        organization_id: &str,
        actor: &str,
        room_id: RoomId,
        seq: i64,
        message_id: Option<MessageId>,
    ) -> ChatResult<ReadCursorCommit> {
        self.repo
            .mark_read_up_to(pool, organization_id, actor, room_id, seq, message_id)
            .await
    }
}
