use super::super::route_prelude::*;
use rust_i18n::*;

#[derive(Template, Clone)]
#[template(path = "settings/ui.gmi", escape = "txt")]
struct UISettingsTemplate {}

pub async fn ui_settings(
    _ctx: RouteContext,
    _user: &'static mut CaracalUser,
) -> Response {
    Response::success(WindTemplate::render(UISettingsTemplate {}))
}

pub async fn ui_settings_chlocale(
    ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let Some(locale) = ctx.parameters.get("locale") else {
        return Response::temporary_failure("No locale");
    };

    let mut ui_config = user.ui_config();
    ui_config.locale = locale.to_string();
    let _ = user.save_ui_config(&ui_config);

    rust_i18n::set_locale(locale);

    Response::success(WindTemplate::render(UISettingsTemplate {}))
}
