//! Model loading for the FMU.
//!
//! Two modes:
//!
//! 1. **Embedded (production / PLC)** — the `export-fmu` codegen step bakes the
//!    model JSON and IO-map JSON into this file as `const` strings. No
//!    filesystem access at runtime, no env vars. This is the mode used for the
//!    `.fmu` shipped to the controller.
//! 2. **Dev** — if the embedded consts are empty, the FMU reads the model from
//!    the `ENTROPYK_FMU_MODEL` and `ENTROPYK_FMU_IO` environment variables (file
//!    paths). Lets you iterate on a model without recompiling.
//!
//! The codegen step overwrites this file's two consts. Keep the function
//! signature stable.

use std::env;

/// Embedded model JSON. Overwritten by `entropyk-cli export-fmu`.
pub const MODEL_JSON: &str = "";
/// Embedded IO-map JSON. Overwritten by `entropyk-cli export-fmu`.
pub const IO_JSON: &str = "";

/// Load the model + IO map. Embedded first, then env vars.
pub fn load_model() -> Result<(String, String), String> {
    if !MODEL_JSON.is_empty() && !IO_JSON.is_empty() {
        return Ok((MODEL_JSON.to_string(), IO_JSON.to_string()));
    }

    let model_path = env::var("ENTROPYK_FMU_MODEL")
        .map_err(|_| "no embedded model and ENTROPYK_FMU_MODEL env var not set".to_string())?;
    let io_path =
        env::var("ENTROPYK_FMU_IO").map_err(|_| "ENTROPYK_FMU_IO env var not set".to_string())?;

    let model =
        std::fs::read_to_string(&model_path).map_err(|e| format!("read {model_path}: {e}"))?;
    let io = std::fs::read_to_string(&io_path).map_err(|e| format!("read {io_path}: {e}"))?;
    Ok((model, io))
}
