pub mod cpu;
pub mod disk;
pub mod disk_io_graph;
pub mod flags;
mod ignore_list;
pub mod layout;
pub mod memory_graph;
pub mod network_graph;
pub mod process;
pub mod style;
pub mod temperature;
pub mod temperature_graph;

use disk::DiskConfig;
use disk_io_graph::DiskIoGraphConfig;
use flags::GeneralConfig;
use network_graph::NetworkGraphConfig;
use serde::{Deserialize, Serialize};
use style::StyleConfig;
use temperature::TempConfig;
use temperature_graph::TempGraphConfig;

pub use self::ignore_list::IgnoreList;
use self::{cpu::CpuConfig, layout::Row, process::ProcessesConfig};
use crate::options::config::memory_graph::MemoryGraphConfig;

/// Overall config for `bottom`.
#[derive(Clone, Debug, Default, Deserialize)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq))]
pub struct Config {
    pub(crate) flags: Option<GeneralConfig>,
    pub(crate) styles: Option<StyleConfig>,
    pub(crate) row: Option<Vec<Row>>,
    pub(crate) processes: Option<ProcessesConfig>,
    pub(crate) disk: Option<DiskConfig>,
    pub(crate) disk_io_graph: Option<DiskIoGraphConfig>,
    pub(crate) temperature: Option<TempConfig>,
    pub(crate) temperature_graph: Option<TempGraphConfig>,
    #[serde(alias = "network")]
    pub(crate) network_graph: Option<NetworkGraphConfig>,
    #[serde(alias = "memory")]
    pub(crate) memory_graph: Option<MemoryGraphConfig>,
    pub(crate) cpu: Option<CpuConfig>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub(crate) enum StringOrNum {
    String(String),
    Num(u64),
}

impl From<String> for StringOrNum {
    fn from(value: String) -> Self {
        StringOrNum::String(value)
    }
}

impl From<u64> for StringOrNum {
    fn from(value: u64) -> Self {
        StringOrNum::Num(value)
    }
}

// I didn't do an invalid config test as a lot of them _are_ valid Config
// when parsed, but fail other checks.
#[cfg(test)]
mod test {

    // Test all valid configs in the integration test folder and ensure they are
    // accepted. We need this separated as only test library code sets
    // `serde(deny_unknown_fields)`.
    #[test]
    #[cfg(feature = "default")]
    fn test_integration_valid_configs() {
        use std::fs;

        use super::Config;

        for config_path in fs::read_dir("./tests/valid_configs").unwrap() {
            let dir_entry = config_path.unwrap();
            let path = dir_entry.path();

            if path.is_file() {
                let config_path_str = path.display().to_string();
                let config_str = fs::read_to_string(path).unwrap();

                toml_edit::de::from_str::<Config>(&config_str)
                    .unwrap_or_else(|_| panic!("incorrectly rejected '{config_path_str}'"));
            }
        }
    }
}
