use std::collections::BTreeMap;

use common::{
    config::{AppConfig, DatabaseConfig, RegionConfig},
    net::ServiceType,
};
use serde::Deserialize;

const DEFAULT_TOML: &str = include_str!("../dbgate.toml");

#[derive(Deserialize, Clone)]
pub struct DbgateConfig {
    pub app_id: u32,
    pub applications: BTreeMap<ServiceType, Vec<AppConfig>>,
    pub database: DatabaseConfig,
    #[allow(dead_code)]
    pub region: RegionConfig,
}

pub fn load() -> Option<DbgateConfig> {
    match common::config::load_or_create_config("dbgate.toml", DEFAULT_TOML) {
        Ok(config) => Some(config),
        Err(err) => {
            tracing::error!("Failed to load config: {err}");
            None
        }
    }
}
