/*
    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>
*/
#![feature(int_roundings, str_as_str, duration_constructors)]
#![allow(clippy::needless_borrows_for_generic_args)]
#![recursion_limit = "4096"]

use config::load_config;
use jellylogic::{admin::log::enable_logging, login::create_admin_account};
use log::{error, info, warn};
use routes::build_rocket;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use std::{path::PathBuf, process::exit, sync::LazyLock};

pub mod api;
pub mod compat;
pub mod config;
pub mod helper;
pub mod logic;
pub mod routes;
pub mod ui;

#[rustfmt::skip]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Config {
    asset_path: PathBuf,
    cookie_key: Option<String>,
    tls:bool,
    hostname: String,
}

pub static CONF_PRELOAD: Mutex<Option<Config>> = Mutex::new(None);
static CONF: LazyLock<Config> = LazyLock::new(|| {
    CONF_PRELOAD
        .lock()
        .unwrap()
        .take()
        .expect("cache config not preloaded. logic error")
});

#[rocket::main]
async fn main() {
    enable_logging();
    info!("loading config...");
    if let Err(e) = load_config().await {
        error!("error {e:?}");
        exit(1);
    }

    #[cfg(feature = "bypass-auth")]
    log::warn!("authentification bypass enabled");

    if let Err(e) = create_admin_account() {
        error!("failed to create admin account: {e:?}");
    }

    let r = build_rocket().launch().await;
    match r {
        Ok(_) => warn!("server shutdown"),
        Err(e) => error!("server exited: {e}"),
    }
}
