/*
    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, page};
use jellycommon::{
    routes::{
        u_admin_import, u_admin_import_post, u_admin_log, u_admin_new_user, u_admin_user,
        u_admin_user_remove, u_admin_users,
    },
    *,
};
use jellyui_locale::tr;

page!(AdminDashboard<'_>, |x| tr(x.ri.lang, "admin.dashboard"));
page!(AdminImport<'_>, |x| tr(x.ri.lang, "admin.import"));
page!(AdminUserList<'_>, |x| tr(x.ri.lang, "admin.users"));
page!(AdminUser<'_>, |x| x
    .user
    .get(USER_NAME)
    .unwrap_or("nameless user")
    .to_string()
    .into());

markup::define!(
    AdminDashboard<'a>(ri: &'a RenderInfo<'a>) {
        h1 { @tr(ri.lang, "admin.dashboard") }
        ul {
            li{a[href=u_admin_log(true)] { @tr(ri.lang, "admin.log.warnonly") }}
            li{a[href=u_admin_log(false)] { @tr(ri.lang, "admin.log.full") }}
        }
        a[href=u_admin_import()] { h2 { @tr(ri.lang, "admin.import") }}
        a[href=u_admin_users()] { h2 { @tr(ri.lang, "admin.users") }}
    }

    AdminImport<'a>(ri: &'a RenderInfo<'a>, errors: &'a [&'a str], busy: bool) {
        @if *busy {
            h1 { @tr(ri.lang, "admin.import.running") }
            noscript { "Live import progress needs javascript." }
            div[id="admin_import"] {}
        } else {
            h1 { @tr(ri.lang, "admin.import") }
            @if !errors.is_empty() {
                section.message.error {
                    details {
                        summary { p.error { @tr(ri.lang, "admin.import_errors").replace("{n}", &errors.len().to_string()) } }
                        ol { @for e in *errors {
                            li.error { pre.error { @e } }
                        }}
                    }
                }
            }
            form[method="POST", action=u_admin_import_post(true)] {
                input[type="submit", value=tr(ri.lang, "admin.dashboard.import.inc").to_string()];
            }
            form[method="POST", action=u_admin_import_post(false)] {
                input[type="submit", value=tr(ri.lang, "admin.dashboard.import.full").to_string()];
            }
        }
    }

    AdminUserList<'a>(ri: &'a RenderInfo<'a>, users: &'a [&'a User]) {
        h1 { @tr(ri.lang, "admin.users") }
        form[method="POST", action=u_admin_new_user()] {
            label[for="login"] { "Login: " }
            input[type="text", id="login", name="login"]; br;
            input[type="submit", value="Create new user"];
        }
        ul { @for u in *users {
            li { a[href=u_admin_user(u.get(USER_LOGIN).unwrap_or_default())] { @u.get(USER_LOGIN) } }
        }}
    }

    AdminUser<'a>(ri: &'a RenderInfo<'a>, user: &'a User) {
        h2 { @user.get(USER_NAME).unwrap_or("nameless user") }
        p { @tr(ri.lang, "tag.Ulgn") ": " @user.get(USER_LOGIN) }
        form[method="POST", action=u_admin_user_remove(user.get(USER_LOGIN).unwrap())] {
            input.danger[type="submit", value="Remove this user(!)"];
        }
    }

);
