use std::io::Write;
use tempfile::NamedTempFile;

#[test]
fn test_config_parse_complet() {
    let mut f = NamedTempFile::new().unwrap();
    write!(f, r#"
[server]
ip = "10.0.0.50"
port = 9999

[protocols.udp]
enabled = true

[protocols.mqtt]
enabled = true
host = "10.0.0.3"
port = 1883
topic_base = "nanometrics/agents"
auto_discovery = true
birth_message = true
last_will = true

[metrics.cpu]
udp = true
mqtt = false
"#).unwrap();
    let cfg = nanometrics_agent::config::load(f.path()).unwrap();
    assert_eq!(cfg.server.ip, "10.0.0.50");
    assert_eq!(cfg.server.port, 9999);
    assert!(cfg.protocols.udp.enabled);
    assert!(cfg.protocols.mqtt.enabled);
    assert_eq!(cfg.protocols.mqtt.host, "10.0.0.3");
    assert!(cfg.metrics.cpu.udp);
    assert!(!cfg.metrics.cpu.mqtt);
}

#[test]
fn test_config_mqtt_absent() {
    let mut f = NamedTempFile::new().unwrap();
    write!(f, r#"
[server]
ip = "10.0.0.50"
port = 9999

[protocols.udp]
enabled = true

[protocols.mqtt]
enabled = false
"#).unwrap();
    let cfg = nanometrics_agent::config::load(f.path()).unwrap();
    assert!(!cfg.protocols.mqtt.enabled);
    // Les valeurs par défaut MQTT doivent être définies
    assert_eq!(cfg.protocols.mqtt.host, "10.0.0.3");
    assert_eq!(cfg.protocols.mqtt.port, 1883);
    assert_eq!(cfg.protocols.mqtt.topic_base, "nanometrics/agents");
}

#[test]
fn test_config_metrics_par_defaut() {
    let mut f = NamedTempFile::new().unwrap();
    write!(f, r#"
[server]
ip = "10.0.0.1"
port = 9999

[protocols.udp]
enabled = true

[protocols.mqtt]
enabled = false
"#).unwrap();
    let cfg = nanometrics_agent::config::load(f.path()).unwrap();
    // Metrics absentes => false par défaut
    assert!(!cfg.metrics.cpu.udp);
    assert!(!cfg.metrics.disk.mqtt);
}
