//! Static informational pages (contact, sitemap, terms, about, stores,
//! shipping). These back the top-bar / footer / sidebar links so none of them
//! is a dead `#`. Content is static; the same chrome context as the home page
//! is supplied so `base.html` (header, cart badge, currencies) renders.

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

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

/// Render one static page through `pages/info.html`, which switches its title +
/// body on the `page` slug. Mirrors `home::index`'s chrome wiring.
async fn render(v: &TeraView, jar: &CookieJar, ctx: &AppContext, page: &str) -> Result<Response> {
    let user = guard::current_user(ctx, jar).await;
    let cur = currency::resolve(ctx, jar).await;
    let c = guard::chrome_from(ctx, user.as_ref());

    format::view(
        v,
        "pages/info.html",
        json!({
            "page": page,
            "logged_in_admin": c.logged_in_admin,
            "logged_in_customer": c.logged_in_customer,
            "customer_name": c.customer_name,
            "customer_account_type": c.customer_account_type,
            "customer_avatar": c.customer_avatar,
            "currency_symbol": cur.symbol,
            "lang": current_lang(jar),
        }),
    )
}

#[debug_handler]
async fn contact(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    render(&v, &jar, &ctx, "contact").await
}

#[debug_handler]
async fn sitemap(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    render(&v, &jar, &ctx, "sitemap").await
}

#[debug_handler]
async fn terms(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    render(&v, &jar, &ctx, "terms").await
}

#[debug_handler]
async fn about(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    render(&v, &jar, &ctx, "about").await
}

#[debug_handler]
async fn stores(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    render(&v, &jar, &ctx, "stores").await
}

#[debug_handler]
async fn shipping(
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    render(&v, &jar, &ctx, "shipping").await
}

pub fn routes() -> Routes {
    Routes::new()
        .add("/kontakt", get(contact))
        .add("/mapa-stranky", get(sitemap))
        .add("/obchodne-podmienky", get(terms))
        .add("/o-nas", get(about))
        .add("/predajne", get(stores))
        .add("/doprava-a-platba", get(shipping))
}
