/*
    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::{
    RenderInfo,
    format::{format_duration, format_duration_long, format_size},
};
use jellycommon::*;
use jellyui_locale::tr;
use markup::raw;

markup::define! {
    StatText<'a>(ri: &'a RenderInfo<'a>, stats: &'a Stats) {
        h1 { @tr(ri.lang, "stats.title") }
        p { @raw(tr(ri.lang, "stats.count")
            .replace("{count}", &format!("<b>{}</b>", stats.all.count))
        )}
        p { @raw(tr(ri.lang, "stats.runtime")
            .replace("{dur}", &format!("<b>{}</b>", format_duration_long(ri.lang, stats.all.sum_duration)))
            .replace("{size}", &format!("<b>{}</b>", format_size(stats.all.sum_size)))
        )}
        p { @raw(tr(ri.lang, "stats.average")
            .replace("{dur}", &format!("<b>{}</b>",  format_duration(stats.all.sum_duration / stats.all.count as f64)))
            .replace("{size}", &format!("<b>{}</b>", format_size(stats.all.sum_size / stats.all.count as u64)))
        )}
        h2 { @tr(ri.lang, "stats.by_kind") }
        table.striped {
            tr {
                th { @tr(ri.lang, "stats.by_kind.kind") }
                th { @tr(ri.lang, "stats.by_kind.count") }
                th { @tr(ri.lang, "stats.by_kind.total_size") }
                th { @tr(ri.lang, "stats.by_kind.total_runtime") }
                th { @tr(ri.lang, "stats.by_kind.average_size") }
                th { @tr(ri.lang, "stats.by_kind.average_runtime") }
                th { @tr(ri.lang, "stats.by_kind.max_size") }
                th { @tr(ri.lang, "stats.by_kind.max_runtime") }
            }
            @for (kind, stat) in &stats.by_kind { tr {
                td { @tr(ri.lang, &format!("tag.kind.{kind}")) }
                td { @stat.count }
                td { @format_size(stat.sum_size) }
                td { @format_duration(stat.sum_duration) }
                td { @format_size(stat.sum_size / stat.count as u64) }
                td { @format_duration(stat.sum_duration / stat.count as f64) }
                td { @format_size(stat.max_size) }
                td { @format_duration(stat.max_duration) }
            }}
        }
    }
}
