/*
    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>
*/
pub mod settings;

use crate::{
    Page,
    locale::{Language, tr, trs},
};
use jellycommon::routes::{u_account_login, u_account_register};

impl Page for AccountLogin<'_> {
    fn title(&self) -> String {
        tr(
            *self.lang,
            if self.logged_in {
                "account.login.switch"
            } else {
                "account.login"
            },
        )
        .to_string()
    }

    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}
impl Page for AccountRegister<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "account.register").to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}
impl Page for AccountRegisterSuccess<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "account.register").to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}
impl Page for AccountLogout<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "account.logout").to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}

markup::define! {
    AccountRegister<'a>(lang: &'a Language) {
        form.account[method="POST", action=""] {
            h1 { @trs(lang, "account.register") }

            label[for="inp-invitation"] { @trs(lang, "account.register.invitation") }
            input[type="text", id="inp-invitation", name="invitation"]; br;

            label[for="inp-username"] { @trs(lang, "account.username") }
            input[type="text", id="inp-username", name="username"]; br;
            label[for="inp-password"] { @trs(lang, "account.password") }
            input[type="password", id="inp-password", name="password"]; br;

            input[type="submit", value=&*tr(**lang, "account.register.submit")];

            p { @trs(lang, "account.register.login") " " a[href=u_account_login()] { @trs(lang, "account.register.login_here") } }
        }
    }
    AccountRegisterSuccess<'a>(lang: &'a Language, logged_in: bool) {
        h1 { @trs(lang, if *logged_in {
            "account.register.success.switch"
        } else {
            "account.register.success"
        })}
    }
    AccountLogin<'a>(lang: &'a Language, logged_in: bool) {
        form.account[method="POST", action=""] {
            h1 { @self.title() }

            label[for="inp-username"] { @trs(lang, "account.username") }
            input[type="text", id="inp-username", name="username"]; br;
            label[for="inp-password"] { @trs(lang, "account.password") }
            input[type="password", id="inp-password", name="password"]; br;

            input[type="submit", value=&*tr(**lang, if *logged_in { "account.login.submit.switch" } else { "account.login.submit" })];

            @if *logged_in {
                p { @trs(lang, "account.login.register.switch") " " a[href=u_account_register()] { @trs(lang, "account.login.register_here") } }
            } else {
                p { @trs(lang, "account.login.cookie_note") }
                p { @trs(lang, "account.login.register") " " a[href=u_account_register()] { @trs(lang, "account.login.register_here") } }
            }
        }
    }
    AccountLogout<'a>(lang: &'a Language) {
        form.account[method="POST", action=""] {
            h1 { @trs(lang, "account.logout") }
            input[type="submit", value=&*tr(**lang, "account.logout.submit")];
        }
    }
}
