/*
    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::{accept::Accept, filter_sort::ANodeFilterSort, RequestInfo};
use jellycommon::api::ApiItemsResponse;
use jellylogic::items::all_items;
use jellyui::{items::ItemsPage, render_page};
use rocket::{get, response::content::RawHtml, serde::json::Json, Either};

#[get("/items?<page>&<filter..>")]
pub fn r_items(
    ri: RequestInfo,
    page: Option<usize>,
    filter: ANodeFilterSort,
) -> Result<Either<RawHtml<String>, Json<ApiItemsResponse>>, MyError> {
    let r = all_items(&ri.session, page, filter.clone().into())?;
    Ok(if matches!(ri.accept, Accept::Json) {
        Either::Right(Json(r))
    } else {
        Either::Left(RawHtml(render_page(
            &ItemsPage {
                lang: &ri.lang,
                r,
                filter: &filter.clone().into(),
                page: page.unwrap_or(0),
            },
            ri.render_info(),
        )))
    })
}
