mod language;
pub use language::*;

mod auth;
pub use auth::*;

mod profile;
pub use profile::*;

mod purchase;
pub use purchase::*;

pub mod proxy;
pub use proxy::*;

mod whitelist;
pub use whitelist::*;

mod promo;
pub use promo::*;

mod general;
pub use general::*;

mod email;
pub use email::*;

use colored_json::*;

pub mod prelude {
    pub use crate::hashmap::HashMap;
    pub use std::time::SystemTime;
    pub use serde::{Serialize, Deserialize};
    pub use color_eyre::{Report, Result};
    pub use std::mem;
    pub use colored::*;
    pub use crate::repository::{
        api::{
            auth::{AUTH_TOKEN, get_auth_token},
            prettify_json_or_text,
            consts::*
        },
        model::*,
    };
}

pub fn prettify_json_or_text(text: &str) -> String {
    match serde_json::from_str::<serde_json::Value>(text) {
        Ok(json) => {
            match serde_json::to_string_pretty(&json) {
                Ok(pretty) => {
                    match pretty.to_colored_json_auto() {
                        Ok(colored) => format!("JSON:\n{}", colored),
                        Err(_) => format!("JSON:\n{}", pretty)
                    }
                }
                Err(_) => format!("Valid JSON (prettify failed): {}", text)
            }
        }
        Err(_) => text.to_string()
    }
}

mod env_var {
    pub const BACK_URL_VAR: &str = "BACKEND_URL";
    pub const BACKEND_USER_VAR: &str = "BACKEND_USERNAME";
    pub const BACKEND_PASSWORD_VAR: &str = "BACKEND_PASSWORD";
}

pub async fn init_envs() {
    use consts::*;
    let _ = &*BACKEND_URL;
    let _ = &*BACKEND_USERNAME;
    let _ = &*BACKEND_PASSWORD;
}

pub mod consts {
    use std::env;
    use crate::repository::api::env_var::*;

    lazy_static::lazy_static! {
        pub static ref BACKEND_URL: String = {
            env::var(BACK_URL_VAR)
                .unwrap_or_else(|_| panic!("Unable to read {BACK_URL_VAR} env"))
        };
        pub static ref BACKEND_API_URL: String = format!("{}/api", *BACKEND_URL);
        pub static ref BACKEND_ADMIN_URL: String = format!("{}/admin", *BACKEND_URL);
        pub static ref BACKEND_USERNAME: String = {
            env::var(BACKEND_USER_VAR)
                .unwrap_or_else(|_| panic!("Unable to read {BACKEND_USER_VAR} env"))
        };
        pub static ref BACKEND_PASSWORD: String = {
            env::var(BACKEND_PASSWORD_VAR)
                .unwrap_or_else(|_| panic!("Unable to read {BACKEND_PASSWORD_VAR} env"))
        };
    }
}