/*
    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, tr, trs},
    scaffold::FlashDisplay,
};
use jellycommon::routes::u_admin_import_post;

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

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