/*
    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::{
    FlashM, Page,
    locale::{Language, trs},
    scaffold::FlashDisplay,
};
use jellycommon::{
    routes::{u_admin_user, u_admin_user_permission, u_admin_user_remove, u_admin_users},
    user::{PermissionSet, User, UserPermission},
};

impl Page for AdminUserPage<'_> {
    fn title(&self) -> String {
        "User Management".to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}
impl Page for AdminUsersPage<'_> {
    fn title(&self) -> String {
        "User Management".to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}

markup::define! {
    AdminUsersPage<'a>(lang: &'a Language, users: &'a [User], flash: &'a FlashM) {
        h1 { @trs(lang, "admin.users.title") }
        @FlashDisplay { flash }
        h2 { @trs(lang, "admin.users.user_list") }
        ul { @for u in *users {
            li {
                a[href=u_admin_user(&u.name)] { @format!("{:?}", u.display_name) " (" @u.name ")" }
            }
        }}
    }
    AdminUserPage<'a>(lang: &'a Language, user: &'a User, flash: &'a FlashM) {
        h1 { @format!("{:?}", user.display_name) " (" @user.name ")" }
        a[href=u_admin_users()] { @trs(lang, "admin.users.return_to_list") }
        @FlashDisplay { flash }
        form[method="POST", action=u_admin_user_remove(&user.name)] {
            // input[type="text", name="name", value=&user.name, hidden];
            input.danger[type="submit", value="Remove user(!)"];
        }

        h2 { "Permissions" }
        @PermissionDisplay { perms: &user.permissions }

        form[method="POST", action=u_admin_user_permission(&user.name)] {
            // input[type="text", name="name", value=&user.name, hidden];
            fieldset.perms {
                legend { "Permission" }
                @for p in UserPermission::ALL_ENUMERABLE {
                    label {
                        input[type="radio", name="permission", value=serde_json::to_string(p).unwrap()];
                        @format!("{p}")
                    } br;
                }
            }
            fieldset.perms {
                legend { "State" }
                label { input[type="radio", name="action", value="unset"]; "Unset" } br;
                label { input[type="radio", name="action", value="grant"]; "Grant" } br;
                label { input[type="radio", name="action", value="revoke"]; "Revoke" } br;
            }
            input[type="submit", value="Update"];
        }
    }
    PermissionDisplay<'a>(perms: &'a PermissionSet) {
        ul { @for (perm,grant) in &perms.0 {
            @if *grant {
                li[class="perm-grant"] { @format!("Allow {}", perm) }
            } else {
                li[class="perm-revoke"] { @format!("Deny {}", perm) }
            }
        }}
    }
}
