//! Manual environment loading with `EnvLoader`.
//!
//! Run with:
//! ```bash
//! export MYAPP__DATABASE__HOST=db.example.com
//! export MYAPP__DATABASE__PORT=5432
//! export MYAPP__CACHE__TTL=3600
//! cargo run --example env_loader
//! ```

use cfgmatic::EnvLoader;

fn main() {
    println!("EnvLoader Example");
    println!();

    // Create a loader with custom prefix and separator
    let loader = EnvLoader::new().prefix("MYAPP").separator("__");

    println!("Environment variable naming:");
    println!(
        "  Database host: {}",
        loader.build_name(&["database", "host"])
    );
    println!(
        "  Database port: {}",
        loader.build_name(&["database", "port"])
    );
    println!("  Cache TTL: {}", loader.build_name(&["cache", "ttl"]));
    println!();

    // Try to load values
    println!("Loading values:");

    match loader.load::<String>(&["database", "host"]) {
        Ok(host) => println!("Database host: {host}"),
        Err(e) => println!("Database host: {e}"),
    }

    match loader.load::<u16>(&["database", "port"]) {
        Ok(port) => println!("Database port: {port}"),
        Err(e) => println!("Database port: {e}"),
    }

    // Try loading optional value
    match loader.try_load::<u64>(&["cache", "ttl"]) {
        Ok(Some(ttl)) => println!("Cache TTL: {ttl} seconds"),
        Ok(None) => println!("Cache TTL: not set"),
        Err(e) => println!("Cache TTL: {e}"),
    }

    // This one likely doesn't exist
    match loader.try_load::<String>(&["optional", "value"]) {
        Ok(Some(v)) => println!("Optional value: {v}"),
        Ok(None) => println!("Optional value: not set (this is OK)"),
        Err(e) => println!("Optional value: {e}"),
    }
}
