// Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use actix_web::web::ServiceConfig;
use serde::Deserialize;
use sqlx::types::Uuid;

pub mod admin;
pub mod bench;
pub mod mcaptcha;
mod meta;
pub mod routes;
pub mod stats;
pub use routes::ROUTES;

pub fn services(cfg: &mut ServiceConfig) {
    meta::services(cfg);
    bench::services(cfg);
    admin::services(cfg);
    mcaptcha::services(cfg);
    stats::services(cfg);
}

pub fn get_random(len: usize) -> String {
    use rand::{distributions::Alphanumeric, rngs::ThreadRng, thread_rng, Rng};
    use std::iter;

    let mut rng: ThreadRng = thread_rng();

    iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(len)
        .collect::<String>()
}

pub fn get_uuid() -> Uuid {
    Uuid::parse_str(&uuid::Uuid::new_v4().to_string()).unwrap()
}

#[derive(Deserialize)]
pub struct RedirectQuery {
    pub redirect_to: Option<String>,
}
