//! I/O side of the notifications service.
//!
//! This is the **only** file inside `service/notifications/` that may perform
//! side effects: reading from the CRDT content store, loading configuration,
//! and spawning the background listener task.

use std::path::Path;

mod listener;
pub use listener::spawn_notification_listener;

/// Subscriber that fires stage-change notifications via the pipeline event bus.
pub(super) mod stage_subscriber;
pub use stage_subscriber::spawn_stage_notification_subscriber;

#[cfg(test)]
mod mock_transport;
#[cfg(test)]
mod tests_notifications;
#[cfg(test)]
mod tests_stage;
#[cfg(test)]
mod tests_transition;

/// Read the story name from the typed CRDT register.
///
/// Returns the name as a `String`, or an empty string if the item is not in
/// the CRDT or has no name set.  Callers that display the name unconditionally
/// should pass this directly to the format_* notification helpers.
pub fn read_story_name(_project_root: &Path, _stage: &str, item_id: &str) -> String {
    crate::crdt_state::read_item(item_id)
        .map(|v| v.name().to_string())
        .unwrap_or_default()
}

/// Look up a story name from the CRDT content store regardless of stage.
///
/// Used for events (like rate-limit warnings) that arrive without a known stage.
fn find_story_name_any_stage(project_root: &Path, item_id: &str) -> String {
    read_story_name(project_root, "", item_id)
}
