//! Toast container — connects to SSE for live notifications.

use crate::primitives::htmx_path;

/// Render the toast container with SSE connection.
pub fn render_toast_container() -> String {
    let sse_url = htmx_path("notifications/sse");
    let ctx = minijinja::context! { sse_url => sse_url };
    rustelo_pages_htmx::fragment_env()
        .and_then(|env| {
            env.get_template("partials/overlays/toast_container.j2")
                .and_then(|t| t.render(ctx))
                .ok()
        })
        .unwrap_or_else(|| {
            format!(
                r##"<div id="toast-stack" class="fixed bottom-4 right-4 flex flex-col gap-2 z-50" aria-live="polite" hx-ext="sse" sse-connect="{url}" sse-swap="toast" hx-swap="beforeend"></div>"##,
                url = html_escape::encode_double_quoted_attribute(&sse_url),
            )
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn opens_sse_to_notifications_endpoint() {
        let html = render_toast_container();
        assert!(html.contains(r#"hx-ext="sse""#));
        assert!(html.contains(r#"sse-connect="/api/htmx/notifications/sse""#));
    }

    #[test]
    fn appends_via_beforeend() {
        assert!(render_toast_container().contains(r#"hx-swap="beforeend""#));
    }
}
