//! Home page renderer.

use std::collections::HashMap;

use minijinja::{Environment, context};
use rustelo_core_lib::load_texts_from_ftl;

use rustelo_pages_htmx::render::render_safe;

pub fn render(env: &Environment, lang: &str) -> String {
    let texts = load_texts(lang);
    render_safe(
        env,
        "pages/home.j2",
        context! { texts => texts, lang => lang },
    )
}

fn load_texts(lang: &str) -> HashMap<String, String> {
    match load_texts_from_ftl(lang) {
        Ok(t) => {
            let map = t.get_translations_for_language(lang);
            tracing::info!(lang, keys = map.len(), "home FTL loaded");
            map
        }
        Err(e) => {
            tracing::error!(lang, error = %e, "home FTL load failed");
            HashMap::new()
        }
    }
}
