use std::collections::BTreeMap;

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

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

#[derive(Deserialize)]
pub struct HttpServerConfig {
    pub bind_host: String,
}

#[derive(Deserialize)]
pub struct NetConfig {
    pub http: HttpServerConfig,
}

#[derive(Deserialize)]
pub struct DispatchConfig {
    pub app_id: u32,
    pub applications: BTreeMap<ServiceType, Vec<AppConfig>>,
    pub net: NetConfig,
    pub database: DatabaseConfig,
    pub region: RegionConfig,
    pub forbid_first_dispatch: bool,
}

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