/*
    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::{
    CONF, FlashM,
    locale::{Language, escape, tr, trs},
};
use jellycommon::{
    routes::{
        u_account_login, u_account_logout, u_account_register, u_account_settings,
        u_admin_dashboard, u_home, u_items, u_node_slug, u_search, u_stats,
    },
    user::User,
};
use markup::{Render, raw};
use std::sync::LazyLock;

static LOGO_ENABLED: LazyLock<bool> = LazyLock::new(|| CONF.asset_path.join("logo.svg").exists());

pub struct RenderInfo {
    pub session: Option<SessionInfo>,
    pub lang: Language,
    pub importing: bool,
}

pub struct SessionInfo {
    pub user: User,
}

markup::define! {
    Scaffold<'a, Main: Render>(title: String, main: Main, class: &'a str, renderinfo: RenderInfo, lang: Language) {
        @markup::doctype()
        html {
            head {
                title { @title " - " @CONF.brand }
                meta[name="viewport", content="width=device-width, initial-scale=1.0"];
                link[rel="stylesheet", href="/assets/style.css"];
                script[src="/assets/bundle.js"] {}
            }
            body[class=class] {
                nav {
                    h1 { a[href=if renderinfo.session.is_some() {u_home()} else {"/".to_string()}] { @if *LOGO_ENABLED { img.logo[src="/assets/logo.svg"]; } else { @CONF.brand } } } " "
                    @if let Some(_) = &renderinfo.session {
                        a.library[href=u_node_slug("library")] { @trs(lang, "nav.root") } " "
                        a.library[href=u_items()] { @trs(lang, "nav.all") } " "
                        a.library[href=u_search()] { @trs(lang, "nav.search") } " "
                        a.library[href=u_stats()] { @trs(lang, "nav.stats") } " "
                        @if renderinfo.importing { span.warn { @trs(lang, "nav.importing") } }
                    }
                    div.account {
                        @if let Some(session) = &renderinfo.session {
                            span { @raw(tr(*lang, "nav.username").replace("{name}", &format!("<b class=\"username\">{}</b>", escape(&session.user.display_name)))) } " "
                            @if session.user.admin {
                                a.admin.hybrid_button[href=u_admin_dashboard()] { p {@trs(lang, "nav.admin")} } " "
                            }
                            a.settings.hybrid_button[href=u_account_settings()] { p {@trs(lang, "nav.settings")} } " "
                            a.logout.hybrid_button[href=u_account_logout()] { p {@trs(lang, "nav.logout")} }
                        } else {
                            a.register.hybrid_button[href=u_account_register()] { p {@trs(lang, "nav.register")} } " "
                            a.login.hybrid_button[href=u_account_login()] { p {@trs(lang, "nav.login")} }
                        }
                    }
                }
                #main { @main }
                footer {
                    p { @CONF.brand " - " @CONF.slogan " | powered by " a[href="https://codeberg.org/metamuffin/jellything"]{"Jellything"} }
                }
            }
        }
    }

    FlashDisplay<'a>(flash: &'a FlashM) {
        @if let Some((kind,message)) = &flash {
            @match kind.as_str() {
                "success" => { section.message { p.success { @message } } }
                "error" => { section.message { p.error { @message } } }
                _ => { section.message { p { @message } } }
            }
        }
    }
}
