//! Public landing page.

use axum_extra::extract::cookie::CookieJar;
use loco_rs::prelude::*;
use serde_json::json;

use crate::{controllers::i18n::current_lang, shared::guard, controllers::shop};

#[debug_handler]
async fn index(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    let products = shop::featured_products(&ctx, 8).await?;
    let (logged_in_admin, logged_in_customer) = guard::chrome(&ctx, &jar).await;

    format::view(
        &v,
        "home/index.html",
        json!({
            "products": products,
            "logged_in_admin": logged_in_admin,
            "logged_in_customer": logged_in_customer,
            "lang": current_lang(&jar),
        }),
    )
}

pub fn routes() -> Routes {
    Routes::new().add("/", get(index))
}
