/*
    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>
*/
pub mod accept;
pub mod cache;
pub mod cors;
pub mod filter_sort;
pub mod language;
pub mod node_id;
pub mod session;
pub mod asset;

use crate::ui::error::{MyError, MyResult};
use accept::Accept;
use jellyimport::is_importing;
use jellylogic::session::Session;
use jellyui::{
    locale::Language,
    scaffold::{RenderInfo, SessionInfo},
};
use language::lang_from_request;
use rocket::{
    async_trait,
    http::Status,
    request::{FromRequest, Outcome},
    Request,
};
use session::session_from_request;

#[derive(Debug, Clone, Copy, Default)]
pub struct A<T>(pub T);

pub struct RequestInfo {
    pub lang: Language,
    pub accept: Accept,
    pub session: Session,
}

impl RequestInfo {
    pub async fn from_request_ut(request: &Request<'_>) -> MyResult<Self> {
        Ok(Self {
            lang: lang_from_request(request),
            accept: Accept::from_request_ut(request),
            session: session_from_request(request).await?,
        })
    }
    pub fn render_info(&self) -> RenderInfo {
        RenderInfo {
            importing: is_importing(),
            session: Some(SessionInfo {
                user: self.session.user.clone(), // TODO no clone?
            }),
            lang: self.lang,
        }
    }
}

#[async_trait]
impl<'r> FromRequest<'r> for RequestInfo {
    type Error = MyError;
    async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
        match Self::from_request_ut(request).await {
            Ok(a) => Outcome::Success(a),
            Err(a) => Outcome::Error((Status::BadRequest, a)),
        }
    }
}
