/*
    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,
    format::{format_duration, format_duration_long, format_kind, format_size},
    locale::{Language, tr, trs},
};
use jellycommon::{
    api::{ApiStatsResponse, StatsBin},
    routes::u_node_slug,
};
use markup::raw;

impl Page for StatsPage<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "stats.title").to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}

markup::define! {
    StatsPage<'a>(lang: &'a Language, r: ApiStatsResponse) {
        .page.stats {
            h1 { @trs(lang, "stats.title") }
            p { @raw(tr(**lang, "stats.count")
                .replace("{count}", &format!("<b>{}</b>", r.total.count))
            )}
            p { @raw(tr(**lang, "stats.runtime")
                .replace("{dur}", &format!("<b>{}</b>", format_duration_long(r.total.runtime, **lang)))
                .replace("{size}", &format!("<b>{}</b>", format_size(r.total.size)))
            )}
            p { @raw(tr(**lang, "stats.average")
                .replace("{dur}", &format!("<b>{}</b>",  format_duration(r.total.average_runtime())))
                .replace("{size}", &format!("<b>{}</b>", format_size(r.total.average_size() as u64)))
            )}

            h2 { @trs(lang, "stats.by_kind.title") }
            table.striped {
                tr {
                    th { @trs(lang, "stats.by_kind.kind") }
                    th { @trs(lang, "stats.by_kind.count") }
                    th { @trs(lang, "stats.by_kind.total_size") }
                    th { @trs(lang, "stats.by_kind.total_runtime") }
                    th { @trs(lang, "stats.by_kind.average_size") }
                    th { @trs(lang, "stats.by_kind.average_runtime") }
                    th { @trs(lang, "stats.by_kind.max_size") }
                    th { @trs(lang, "stats.by_kind.max_runtime") }
                }
                @for (k,b) in &r.kinds { tr {
                    td { @format_kind(*k, **lang) }
                    td { @b.count }
                    td { @format_size(b.size) }
                    td { @format_duration(b.runtime) }
                    td { @format_size(b.average_size() as u64) }
                    td { @format_duration(b.average_runtime()) }
                    td { @if b.max_size.0 > 0 { a[href=u_node_slug(&b.max_size.1)]{ @format_size(b.max_size.0) }}}
                    td { @if b.max_runtime.0 > 0. { a[href=u_node_slug(&b.max_runtime.1)]{ @format_duration(b.max_runtime.0) }}}
                }}
            }
        }
    }
}

trait BinExt {
    fn average_runtime(&self) -> f64;
    fn average_size(&self) -> f64;
}
impl BinExt for StatsBin {
    fn average_runtime(&self) -> f64 {
        self.runtime / self.count as f64
    }
    fn average_size(&self) -> f64 {
        self.size as f64 / self.count as f64
    }
}
