use std::collections::BTreeMap;

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

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

#[derive(Deserialize, Clone)]
pub struct PlayerServerConfig {
    pub outer_ip: IpString,
    pub listen_port: u16,
    pub max_conn_num: usize,
    pub recv_buf_len: usize,
    #[allow(dead_code)]
    pub timeout_sec: u32,
    pub mtu: usize,
}

#[derive(Deserialize, Clone)]
pub struct NetConfig {
    pub player_server: PlayerServerConfig,
}

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

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