/*
    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 account;
pub mod admin;
pub mod api;
pub mod assets;
pub mod compat;
pub mod error;
pub mod home;
pub mod index;
pub mod items;
pub mod node;
pub mod player;
pub mod playersync;
pub mod stream;
pub mod style;
pub mod userdata;

use self::{
    account::{
        r_account_login, r_account_login_post, r_account_logout, r_account_logout_post,
        settings::{r_account_settings, r_account_settings_post},
    },
    admin::{
        import::{r_admin_import, r_admin_import_post, r_admin_import_stream},
        log::{r_admin_log, r_admin_log_stream},
        r_admin_dashboard,
        users::{r_admin_new_user, r_admin_user, r_admin_user_remove, r_admin_users},
    },
    api::{r_api_root, r_version},
    assets::{r_image, r_image_fallback_person},
    compat::youtube::{r_youtube_channel, r_youtube_embed, r_youtube_watch},
    error::{r_api_catch, r_catch},
    home::r_home,
    index::{r_favicon, r_index},
    items::r_items,
    node::r_node,
    player::r_player,
    playersync::{PlayersyncChannels, r_playersync},
    stream::r_stream,
    style::{r_assets_css, r_assets_font, r_assets_js, r_assets_js_map},
};
use crate::State;
use rocket::{
    Build, Config, Rocket, catchers, fairing::AdHoc, fs::FileServer, http::Header, routes,
    shield::Shield,
};
use std::sync::Arc;

#[macro_export]
macro_rules! uri {
    ($kk:stmt) => {
        &rocket::uri!($kk).to_string()
    };
}

pub(super) fn build_rocket(state: Arc<State>) -> Rocket<Build> {
    rocket::build()
        .configure(Config {
            address: std::env::var("BIND_ADDR")
                .map(|e| e.parse().unwrap())
                .unwrap_or("127.0.0.1".parse().unwrap()),
            port: std::env::var("PORT")
                .map(|e| e.parse().unwrap())
                .unwrap_or(8000),
            ip_header: Some("x-real-ip".into()),
            ..Default::default()
        })
        .manage(PlayersyncChannels::default())
        .manage(state.clone())
        .attach(AdHoc::on_response("set server header", |_req, res| {
            res.set_header(Header::new("server", "jellything"));
            Box::pin(async {})
        }))
        .attach(AdHoc::on_response("frame options", |req, resp| {
            if !req.uri().path().as_str().starts_with("/embed") {
                resp.set_raw_header("X-Frame-Options", "SAMEORIGIN");
            }
            Box::pin(async {})
        }))
        .attach(Shield::new())
        .register("/", catchers![r_catch])
        .register("/api", catchers![r_api_catch])
        .mount("/assets", FileServer::from(&state.config.asset_path))
        .mount(
            "/",
            routes![
                r_account_login_post,
                r_account_login,
                r_account_logout_post,
                r_account_logout,
                r_account_settings_post,
                r_account_settings,
                r_admin_dashboard,
                r_admin_import_post,
                r_admin_import_stream,
                r_admin_import,
                r_admin_log_stream,
                r_admin_log,
                r_admin_new_user,
                r_admin_users,
                r_admin_user,
                r_admin_user_remove,
                r_api_root,
                r_assets_css,
                r_assets_font,
                r_assets_js_map,
                r_assets_js,
                r_favicon,
                r_home,
                r_image_fallback_person,
                r_image,
                r_index,
                r_items,
                r_node,
                r_player,
                r_playersync,
                r_stream,
                r_version,
                // Compat
                // r_jellyfin_artists,
                // r_jellyfin_branding_configuration,
                // r_jellyfin_branding_css,
                // r_jellyfin_displaypreferences_usersettings_post,
                // r_jellyfin_displaypreferences_usersettings,
                // r_jellyfin_items_image_primary,
                // r_jellyfin_items_images_backdrop,
                // r_jellyfin_items_intros,
                // r_jellyfin_items_item,
                // r_jellyfin_items_playbackinfo,
                // r_jellyfin_items_similar,
                // r_jellyfin_items,
                // r_jellyfin_livetv_programs_recommended,
                // r_jellyfin_persons,
                // r_jellyfin_playback_bitratetest,
                // r_jellyfin_quickconnect_enabled,
                // r_jellyfin_sessions_capabilities_full,
                // r_jellyfin_sessions_playing_progress,
                // r_jellyfin_sessions_playing,
                // r_jellyfin_shows_nextup,
                // r_jellyfin_socket,
                // r_jellyfin_system_endpoint,
                // r_jellyfin_system_info_public_case,
                // r_jellyfin_system_info_public,
                // r_jellyfin_system_info,
                // r_jellyfin_users_authenticatebyname,
                // r_jellyfin_users_authenticatebyname_case,
                // r_jellyfin_users_id,
                // r_jellyfin_users_items_item,
                // r_jellyfin_users_items,
                // r_jellyfin_users_public,
                // r_jellyfin_users_views,
                // r_jellyfin_video_stream,
                r_youtube_channel,
                r_youtube_embed,
                r_youtube_watch,
            ],
        )
}
