/*
    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 super::error::MyError;
use crate::helper::RequestInfo;
use jellycommon::api::ApiStatsResponse;
use jellylogic::stats::stats;
use jellyui::{render_page, stats::StatsPage};
use rocket::{get, response::content::RawHtml, serde::json::Json, Either};

#[get("/stats")]
pub fn r_stats(
    ri: RequestInfo,
) -> Result<Either<RawHtml<String>, Json<ApiStatsResponse>>, MyError> {
    let r = stats(&ri.session)?;

    Ok(if ri.accept.is_json() {
        Either::Right(Json(r))
    } else {
        Either::Left(RawHtml(render_page(
            &StatsPage { lang: &ri.lang, r },
            ri.render_info(),
        )))
    })
}
