use crate::web::MyResponse;
use crate::web::maintenance::JsonErrorMessage;
use crate::web::maintenance::templates::MaintenanceMode;
use crate::web::util::get_commit_sha;
use rocket_codegen::get;
use rocket_dyn_templates::Template;
use rocket_i18n::I18n;
use serde_json::json;

#[get("/maintenance/plain/<message>")]
pub fn maintenance_error_plain(message: String) -> MyResponse {
    MyResponse::MaintenancePlain(message)
}

#[get("/maintenance/json/<message>")]
pub fn maintenance_error_json(message: String) -> MyResponse {
    MyResponse::MaintenanceJson(json!(JsonErrorMessage { message }))
}

#[get("/maintenance/web/<message>")]
pub fn maintenance_error_web(message: String, i18n: I18n) -> MyResponse {
    let ctx = MaintenanceMode {
        message,
        version: env!("VERGEN_SEMVER").to_string(),
        commit: get_commit_sha(),
        lang: i18n.lang.to_owned(),
    };
    MyResponse::Maintenance(Template::render("maintenance", ctx))
}
