use std::env;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    const ENV_VAR_NAME: &str = "DEV";
    // 1. Attempt to get the environment variable's value.
    match env::var(ENV_VAR_NAME) {
        // 2. If the variable is successfully retrieved (Ok(value)), check if the value is "1".
        Ok(value) if value == "1" => {
            println!(
                "cargo:warning=Condition met: {} is set to '1'.",
                ENV_VAR_NAME
            );

            // 3. If the value is "1", call the function and use '?' to propagate errors.
            generate_headers()?;
        }
        // 4. If the variable is set to any other value, or not set (Err), skip.
        _ => {
            println!(
                "cargo:warning=Skipping header generation. Set {}='1' to enable.",
                ENV_VAR_NAME
            );
        }
    }
    // Declare `loom` as a known cfg key to avoid `unexpected_cfgs` warnings
    // when building with `RUSTFLAGS="--cfg loom"`.
    println!("cargo:rustc-check-cfg=cfg(loom)");
    Ok(())
}

fn generate_headers() -> Result<(), Box<dyn std::error::Error>> {
    let crate_dir = std::env::var("CARGO_MANIFEST_DIR")?;
    // Generate C header using cbindgen.toml
    match cbindgen::generate(&crate_dir) {
        Ok(bindings) => {
            bindings.write_to_file("dtact.h");
            println!("cargo:warning=Generated dtact.h");
        }
        Err(e) => {
            println!(
                "cargo:warning=Failed \
                 to generate C \
                 bindings: {:?}",
                e
            );
            println!("cargo:warning=Continuing build without C header generation");
        }
    }

    // Generate C++ header with custom config
    let cpp_config = cbindgen::Config {
        language: cbindgen::Language::Cxx,
        namespace: Some("dtact".to_string()),
        ..cbindgen::Config::from_file("cbindgen-cxx.toml").unwrap_or_default()
    };

    match cbindgen::Builder::new()
        .with_crate(&crate_dir)
        .with_config(cpp_config)
        .generate()
    {
        Ok(bindings) => {
            bindings.write_to_file("dtact.hpp");

            println!("cargo:warning=Generated dtact.hpp");
        }
        Err(e) => {
            println!(
                "cargo:warning=Failed \
                 to generate C++ \
                 bindings: {:?}",
                e
            );
            println!("cargo:warning=Continuing build without C++ header generation");
        }
    }

    Ok(())
}
