use crate::shell::shell_with_path;
use leptos::prelude::Owner;
use leptos_config::LeptosOptions;

/// Renders the SSR shell for a given request path.
///
/// Registered with `rustelo_server` so all SSR responses go through this
/// implementation's shell (layout, theme, navigation) rather than the
/// framework's generic default.
pub fn render_ssr_with_context(options: LeptosOptions, path: Option<String>) -> String {
    let owner = Owner::new();
    owner.with(|| shell_with_path(options, path).to_html())
}

/// Delegates to `rustelo_server`'s Axum runner.
pub async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
    rustelo_server::run::run_server().await
}

/// Application entry point: initialises services, registers resources,
/// and starts the Axum server on a local-task Tokio runtime.
pub fn run_main() -> Result<(), Box<dyn std::error::Error>> {
    rustelo_server::utils::init();
    crate::resources::initialize()?;
    let rt = tokio::runtime::LocalRuntime::new()?;
    rt.block_on(run_server())
}
