//! Exemple: Ports et connexions
//!
//! Démonstration du Type-State pattern pour les ports thermodynamiques.
//!
//! Exécuter: cargo run -p entropyk-demo --bin ports

use entropyk_components::port::{ConnectionError, FluidId, Port};
use entropyk_core::{Enthalpy, Pressure};

fn main() -> Result<(), ConnectionError> {
    println!("=== Exemple: Ports et Connexions ===\n");

    let port1 = Port::new(
        FluidId::new("R134a"),
        Pressure::from_bar(1.0),
        Enthalpy::from_joules_per_kg(400_000.0),
    );

    let port2 = Port::new(
        FluidId::new("R134a"),
        Pressure::from_bar(1.0),
        Enthalpy::from_joules_per_kg(400_000.0),
    );

    println!(
        "Port 1: fluide={}, P={:.2} bar, h={:.0} J/kg",
        port1.fluid_id(),
        port1.pressure().to_bar(),
        port1.enthalpy().to_joules_per_kg()
    );

    let (mut connected1, _connected2) = port1.connect(port2)?;
    println!("\n✅ Ports connectés avec succès!");

    connected1.set_pressure(Pressure::from_bar(1.5));
    connected1.set_enthalpy(Enthalpy::from_joules_per_kg(450_000.0));

    println!(
        "Port 1 modifié: P={:.2} bar, h={:.0} J/kg",
        connected1.pressure().to_bar(),
        connected1.enthalpy().to_joules_per_kg()
    );

    Ok(())
}
