use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
    // Tracing setup
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "info".into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    // Limit worker threads for CPU control (can be tuned via env)
    // Note: When using #[tokio::main], configure via env TOKIO_WORKER_THREADS.
    // Alternatively, build a Runtime manually for stricter control.
    if let Ok(threads) = std::env::var("TOKIO_WORKER_THREADS") {
        tracing::warn!(
            "TOKIO_WORKER_THREADS is set to {threads}, ensure it matches deployment requirements"
        );
    } else {
        // Provide a sane default via env if not set
        let default_threads = std::cmp::max(1, num_cpus::get_physical() / 2);
        unsafe { std::env::set_var("TOKIO_WORKER_THREADS", default_threads.to_string()); }
        tracing::info!("Defaulting worker threads to {}", default_threads);
    }

    // Example: lazily load and invoke the "summarizer" module when needed
    let host = owly_news_module_host::ModuleHost::default();

    // Simulate an on-demand call (e.g., from an HTTP handler)
    let summarizer = host.get("summarizer").await?;
    let resp = summarizer.invoke_json(
        "summarize",
        serde_json::json!({
            "text": "Rust enables fearless concurrency with strong guarantees over memory safety.",
            "ratio": 0.3
        }),
    )?;
    tracing::info!(?resp, "summarizer response");

    // TODO: wire this into your API routes/handlers, using the host.get("<module>").await when needed.
    tracing::info!("owly-news daemon running");
    Ok(())
}
