use serde::{Deserialize, Serialize};

/// Traceability metadata for a simulation result.
///
/// Satisfies AC3 (structured JSON): use [`Self::to_json`] or
/// `serde_json::to_string(&metadata)` to obtain JSON representation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SimulationMetadata {
    /// Version of the solver crate used.
    pub solver_version: String,
    /// Version of the fluid backend used.
    ///
    /// Currently a fixed placeholder. In a full deployment this could be
    /// queried from `entropyk_fluids` or CoolProp for accurate traceability.
    pub fluid_backend_version: String,
    /// SHA-256 hash of the input configuration uniquely identifying the system configuration.
    pub input_hash: String,
}

impl SimulationMetadata {
    /// Create a new SimulationMetadata with the given input hash.
    ///
    /// `solver_version` is set from `CARGO_PKG_VERSION`; `fluid_backend_version`
    /// is a placeholder until backend version reporting is integrated.
    pub fn new(input_hash: String) -> Self {
        Self {
            solver_version: env!("CARGO_PKG_VERSION").to_string(),
            fluid_backend_version: "0.1.0".to_string(),
            input_hash,
        }
    }

    /// Returns the metadata as a JSON string (pretty-printed).
    ///
    /// Use this for logging, persistence, or API responses when structured
    /// JSON is required (AC3).
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }
}
