/*
    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::{
    Page,
    locale::{Language, tr, trs},
    node_card::NodeCard,
};
use jellycommon::api::ApiSearchResponse;
use markup::DynRender;

impl Page for SearchPage<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "search.title").to_string()
    }
    fn class(&self) -> Option<&'static str> {
        Some("search")
    }
    fn to_render(&self) -> DynRender<'_> {
        markup::new!(@self)
    }
}

markup::define! {
    SearchPage<'a>(lang: &'a Language, r: Option<ApiSearchResponse>, query: &'a Option<String>) {
        h1 { @trs(lang, "search.title") }
        form[action="", method="GET"] {
            input[type="text", name="query", placeholder=&*tr(**lang, "search.placeholder"), value=&query];
            input[type="submit", value="Search"];
        }
        @if let Some(r) = &r {
            h2 { @trs(lang, "search.results.title") }
            p.stats { @tr(**lang, "search.results.stats").replace("{count}", &r.count.to_string()).replace("{dur}", &format!("{:?}", r.duration)) }
            ul.children {@for (node, udata) in r.results.iter() {
                li { @NodeCard { node, udata, lang } }
            }}
            // TODO pagination
        }
    }
}
