//! Loading with custom environment prefix.
//!
//! Run with:
//! ```bash
//! export PRODUCTION__PORT=443
//! export PRODUCTION__TLS=true
//! cargo run --example custom_prefix
//! ```

use cfgmatic::{Config, ConfigLoader};
use serde::Deserialize;

#[derive(Debug, Default, Deserialize, Config)]
struct ServerConfig {
    #[config(default = "8080")]
    port: u16,
    #[config(default = "false")]
    tls: bool,
    #[config(default = "/etc/ssl/cert.pem")]
    cert_path: String,
}

fn main() {
    println!("Custom Prefix Example");
    println!();

    // The struct defines default prefix "SERVERCONFIG" (from struct name)
    // But we can override it at load time

    println!("Loading with default prefix (SERVERCONFIG):");
    let default_config = ServerConfig::from_env();
    println!("  {default_config:?}\n");

    println!("Loading with custom prefix 'PRODUCTION':");
    let production =
        ServerConfig::from_env_with_prefix("PRODUCTION").expect("Failed to load production config");

    println!("  Port: {}", production.port);
    println!("  TLS: {}", production.tls);
    println!("  Cert: {}", production.cert_path);

    println!("\nLoading with custom prefix 'DEVELOPMENT':");
    let development = ServerConfig::from_env_with_prefix("DEVELOPMENT").unwrap_or_else(|_| {
        println!("  (Using defaults since DEVELOPMENT__* vars not set)");
        ServerConfig::default()
    });

    println!("  Port: {}", development.port);
    println!("  TLS: {}", development.tls);
}
