/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::RenderInfo;
use jellycommon::{
    routes::{
        u_account_login, u_account_logout, u_account_settings, u_admin_dashboard, u_home, u_items,
        u_node_slug, u_search, u_stats,
    },
    user::{USER_ADMIN, USER_NAME},
};
use jellyui_locale::{escape, tr};
use markup::{Render, raw};

markup::define! {
    Scaffold<'a, Main: Render>(ri: &'a RenderInfo<'a>, title: &'a str, main: Main, class: &'a str) {
        @markup::doctype()
        html {
            head {
                title { @title " - " @ri.config.brand }
                meta[name="viewport", content="width=device-width, initial-scale=1.0"];
                link[rel="stylesheet", href="/assets/bundle.css"];
                script[src="/assets/bundle.js"] {}
            }
            body[class=class] {
                @Navbar { ri }
                #main { @main }
                footer {
                    p { @ri.config.brand " - " @ri.config.slogan " | powered by " a[href="https://codeberg.org/metamuffin/jellything"]{"Jellything"} }
                }
            }
        }
    }

    Navbar<'a>(ri: &'a RenderInfo<'a>) {
        nav {
            h1 { a[href=if ri.user.is_some() {u_home()} else {"/".to_string()}] { @if ri.config.logo { img.logo[src="/assets/logo.svg"]; } else { @ri.config.brand } } } " "
            @if ri.user.is_some() {
                a.library[href=u_node_slug("library")] { @tr(ri.lang, "nav.root") } " "
                a.library[href=u_items()] { @tr(ri.lang, "nav.all") } " "
                a.library[href=u_search()] { @tr(ri.lang, "nav.search") } " "
                a.library[href=u_stats()] { @tr(ri.lang, "nav.stats") } " "
                @if let Some(m) = ri.status_message { span.warn { @tr(ri.lang, m) } }
            }
            div.account {
                @if let Some(user) = &ri.user {
                    span { @raw(tr(ri.lang, "nav.username").replace("{name}", &format!("<b class=\"username\">{}</b>", escape(user.get(USER_NAME).unwrap_or("nameless user"))))) } " "
                    @if user.has(USER_ADMIN.0) {
                        a.admin.hybrid_button[href=u_admin_dashboard()] { p {@tr(ri.lang, "nav.admin")} } " "
                    }
                    a.settings.hybrid_button[href=u_account_settings()] { p {@tr(ri.lang, "nav.settings")} } " "
                } else {
                    a.login.hybrid_button[href=u_account_login()] { p {@tr(ri.lang, "nav.login")} }
                }
            }
        }
    }
}
