use lazy_static::lazy_static;
use serde::Deserialize;
use std::fs;
use std::process::exit;

#[derive(Debug, Deserialize)]
pub struct NodeConfig {
    role: NodeRole,
}

#[derive(Debug, Deserialize)]
pub enum NodeRole {
    HeadNode,
    ChildNode,
}

#[derive(Debug, Deserialize)]
pub struct LogConfig {
    pub level: String,
}

#[derive(Debug, Deserialize)]
pub struct Config {
    pub node: NodeConfig,
    pub log: LogConfig,
}

fn load_config() -> Config {
    let filename = "config.toml";
    let contents = match fs::read_to_string(filename) {
        Ok(c) => c,
        Err(_) => {
            eprintln!("[-][ERROR] Could not read config file `{}`", filename);
            exit(1);
        }
    };

    let config: Config = match toml::from_str(&contents) {
        Ok(conf) => conf,
        Err(err) => {
            eprintln!("Unable to load data from file `{}`", filename);
            eprintln!("[-][ERROR] {}", err.message());
            exit(1);
        }
    };

    config
}

lazy_static! {
    pub static ref CONFIG: Config = load_config();
}