/*
    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::MyResult;
use crate::request_info::RequestInfo;
use anyhow::anyhow;
use jellycommon::{
    jellyobject::{EMPTY, Path},
    *,
};
use jellydb::{Filter, Query};
use jellyui::components::node_page::Player;
use rocket::{get, response::content::RawHtml};
use std::borrow::Cow;

// fn jellynative_url(action: &str, seek: f64, secret: &str, node: &str, session: &str) -> String {
//     let protocol = if CONF.tls { "https" } else { "http" };
//     let host = &CONF.hostname;
//     let stream_url = format!(
//         "/n/{node}/stream{}",
//         StreamSpec::HlsMultiVariant {
//             container: StreamContainer::Matroska
//         }
//         .to_query()
//     );
//     format!("jellynative://{action}/{secret}/{session}/{seek}/{protocol}://{host}{stream_url}",)
// }

#[get("/n/<slug>/player?<t>", rank = 4)]
pub fn r_player(ri: RequestInfo<'_>, t: Option<f64>, slug: &str) -> MyResult<RawHtml<String>> {
    ri.require_user()?;
    let _ = t;

    let mut node = None;
    ri.state.database.transaction(&mut |txn| {
        if let Some(row) = txn.query_single(Query {
            filter: Filter::Match(Path(vec![NO_SLUG.0]), slug.into()),
            ..Default::default()
        })? {
            node = Some(txn.get(row)?.unwrap());
        }
        Ok(())
    })?;
    let Some(node) = node else {
        Err(anyhow!("no such node"))?
    };

    Ok(ri.respond_ui(&Player {
        ri: &ri.render_info(),
        nku: Nku {
            node: Cow::Borrowed(&node),
            userdata: Cow::Borrowed(EMPTY),
            role: None,
        },
    }))
}
