//! Python wrappers for Entropyk core physical types.
//!
//! Each wrapper holds the inner Rust NewType and exposes Pythonic constructors
//! with keyword arguments (e.g., `Pressure(bar=1.0)`) plus unit conversion methods.

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

// =============================================================================
// Pressure
// =============================================================================

/// Pressure in Pascals (Pa).
///
/// Construct with one of: ``pa``, ``bar``, ``kpa``, ``psi``.
///
/// Example::
///
///     p = Pressure(bar=1.0)
///     print(p.to_bar())   # 1.0
///     print(float(p))     # 100000.0
#[pyclass(name = "Pressure", module = "entropyk")]
#[derive(Clone)]
pub struct PyPressure {
    pub(crate) inner: entropyk::Pressure,
}

#[pymethods]
impl PyPressure {
    /// Create a Pressure. Specify exactly one of: ``pa``, ``bar``, ``kpa``, ``psi``.
    #[new]
    #[pyo3(signature = (pa=None, bar=None, kpa=None, psi=None))]
    fn new(
        pa: Option<f64>,
        bar: Option<f64>,
        kpa: Option<f64>,
        psi: Option<f64>,
    ) -> PyResult<Self> {
        let value = match (pa, bar, kpa, psi) {
            (Some(v), None, None, None) => v,
            (None, Some(v), None, None) => v * 100_000.0,
            (None, None, Some(v), None) => v * 1_000.0,
            (None, None, None, Some(v)) => v * 6894.75729,
            _ => {
                return Err(PyValueError::new_err(
                    "Specify exactly one of: pa, bar, kpa, psi",
                ))
            }
        };
        Ok(PyPressure {
            inner: entropyk::Pressure(value),
        })
    }

    /// Value in Pascals.
    fn to_pascals(&self) -> f64 {
        self.inner.to_pascals()
    }

    /// Value in bar.
    fn to_bar(&self) -> f64 {
        self.inner.to_bar()
    }

    /// Value in kPa.
    fn to_kpa(&self) -> f64 {
        self.inner.0 / 1_000.0
    }

    /// Value in PSI.
    fn to_psi(&self) -> f64 {
        self.inner.to_psi()
    }

    fn __repr__(&self) -> String {
        format!(
            "Pressure({:.2} Pa = {:.4} bar)",
            self.inner.0,
            self.inner.0 / 100_000.0
        )
    }

    fn __str__(&self) -> String {
        format!("{:.2} Pa", self.inner.0)
    }

    fn __float__(&self) -> f64 {
        self.inner.0
    }

    fn __eq__(&self, other: &PyPressure) -> bool {
        (self.inner.0 - other.inner.0).abs() < 1e-10
    }

    fn __add__(&self, other: &PyPressure) -> PyPressure {
        PyPressure {
            inner: self.inner + other.inner,
        }
    }

    fn __sub__(&self, other: &PyPressure) -> PyPressure {
        PyPressure {
            inner: self.inner - other.inner,
        }
    }
}

// =============================================================================
// Temperature
// =============================================================================

/// Temperature in Kelvin (K).
///
/// Construct with one of: ``kelvin``, ``celsius``, ``fahrenheit``.
///
/// Example::
///
///     t = Temperature(celsius=25.0)
///     print(t.to_kelvin())     # 298.15
///     print(t.to_celsius())    # 25.0
#[pyclass(name = "Temperature", module = "entropyk")]
#[derive(Clone)]
pub struct PyTemperature {
    pub(crate) inner: entropyk::Temperature,
}

#[pymethods]
impl PyTemperature {
    /// Create a Temperature. Specify exactly one of: ``kelvin``, ``celsius``, ``fahrenheit``.
    #[new]
    #[pyo3(signature = (kelvin=None, celsius=None, fahrenheit=None))]
    fn new(kelvin: Option<f64>, celsius: Option<f64>, fahrenheit: Option<f64>) -> PyResult<Self> {
        let inner = match (kelvin, celsius, fahrenheit) {
            (Some(v), None, None) => entropyk::Temperature::from_kelvin(v),
            (None, Some(v), None) => entropyk::Temperature::from_celsius(v),
            (None, None, Some(v)) => entropyk::Temperature::from_fahrenheit(v),
            _ => {
                return Err(PyValueError::new_err(
                    "Specify exactly one of: kelvin, celsius, fahrenheit",
                ))
            }
        };
        Ok(PyTemperature { inner })
    }

    /// Value in Kelvin.
    fn to_kelvin(&self) -> f64 {
        self.inner.to_kelvin()
    }

    /// Value in Celsius.
    fn to_celsius(&self) -> f64 {
        self.inner.to_celsius()
    }

    /// Value in Fahrenheit.
    fn to_fahrenheit(&self) -> f64 {
        self.inner.to_fahrenheit()
    }

    fn __repr__(&self) -> String {
        format!(
            "Temperature({:.2} K = {:.2} °C)",
            self.inner.0,
            self.inner.0 - 273.15
        )
    }

    fn __str__(&self) -> String {
        format!("{:.2} K", self.inner.0)
    }

    fn __float__(&self) -> f64 {
        self.inner.0
    }

    fn __eq__(&self, other: &PyTemperature) -> bool {
        (self.inner.0 - other.inner.0).abs() < 1e-10
    }

    fn __add__(&self, other: &PyTemperature) -> PyTemperature {
        PyTemperature {
            inner: entropyk::Temperature(self.inner.0 + other.inner.0),
        }
    }

    fn __sub__(&self, other: &PyTemperature) -> PyTemperature {
        PyTemperature {
            inner: entropyk::Temperature(self.inner.0 - other.inner.0),
        }
    }
}

// =============================================================================
// Enthalpy
// =============================================================================

/// Specific enthalpy in J/kg.
///
/// Construct with one of: ``j_per_kg``, ``kj_per_kg``.
///
/// Example::
///
///     h = Enthalpy(kj_per_kg=250.0)
///     print(h.to_kj_per_kg())  # 250.0
#[pyclass(name = "Enthalpy", module = "entropyk")]
#[derive(Clone)]
pub struct PyEnthalpy {
    pub(crate) inner: entropyk::Enthalpy,
}

#[pymethods]
impl PyEnthalpy {
    /// Create an Enthalpy. Specify exactly one of: ``j_per_kg``, ``kj_per_kg``.
    #[new]
    #[pyo3(signature = (j_per_kg=None, kj_per_kg=None))]
    fn new(j_per_kg: Option<f64>, kj_per_kg: Option<f64>) -> PyResult<Self> {
        let inner = match (j_per_kg, kj_per_kg) {
            (Some(v), None) => entropyk::Enthalpy::from_joules_per_kg(v),
            (None, Some(v)) => entropyk::Enthalpy::from_kilojoules_per_kg(v),
            _ => {
                return Err(PyValueError::new_err(
                    "Specify exactly one of: j_per_kg, kj_per_kg",
                ))
            }
        };
        Ok(PyEnthalpy { inner })
    }

    /// Value in J/kg.
    fn to_j_per_kg(&self) -> f64 {
        self.inner.to_joules_per_kg()
    }

    /// Value in kJ/kg.
    fn to_kj_per_kg(&self) -> f64 {
        self.inner.to_kilojoules_per_kg()
    }

    fn __repr__(&self) -> String {
        format!(
            "Enthalpy({:.2} J/kg = {:.2} kJ/kg)",
            self.inner.0,
            self.inner.0 / 1_000.0
        )
    }

    fn __str__(&self) -> String {
        format!("{:.2} J/kg", self.inner.0)
    }

    fn __float__(&self) -> f64 {
        self.inner.0
    }

    fn __eq__(&self, other: &PyEnthalpy) -> bool {
        (self.inner.0 - other.inner.0).abs() < 1e-10
    }

    fn __add__(&self, other: &PyEnthalpy) -> PyEnthalpy {
        PyEnthalpy {
            inner: entropyk::Enthalpy(self.inner.0 + other.inner.0),
        }
    }

    fn __sub__(&self, other: &PyEnthalpy) -> PyEnthalpy {
        PyEnthalpy {
            inner: entropyk::Enthalpy(self.inner.0 - other.inner.0),
        }
    }
}

// =============================================================================
// MassFlow
// =============================================================================

/// Mass flow rate in kg/s.
///
/// Construct with one of: ``kg_per_s``, ``g_per_s``.
///
/// Example::
///
///     m = MassFlow(kg_per_s=0.5)
///     print(m.to_g_per_s())  # 500.0
#[pyclass(name = "MassFlow", module = "entropyk")]
#[derive(Clone)]
pub struct PyMassFlow {
    pub(crate) inner: entropyk::MassFlow,
}

#[pymethods]
impl PyMassFlow {
    /// Create a MassFlow. Specify exactly one of: ``kg_per_s``, ``g_per_s``.
    #[new]
    #[pyo3(signature = (kg_per_s=None, g_per_s=None))]
    fn new(kg_per_s: Option<f64>, g_per_s: Option<f64>) -> PyResult<Self> {
        let inner = match (kg_per_s, g_per_s) {
            (Some(v), None) => entropyk::MassFlow::from_kg_per_s(v),
            (None, Some(v)) => entropyk::MassFlow::from_grams_per_s(v),
            _ => {
                return Err(PyValueError::new_err(
                    "Specify exactly one of: kg_per_s, g_per_s",
                ))
            }
        };
        Ok(PyMassFlow { inner })
    }

    /// Value in kg/s.
    fn to_kg_per_s(&self) -> f64 {
        self.inner.to_kg_per_s()
    }

    /// Value in g/s.
    fn to_g_per_s(&self) -> f64 {
        self.inner.to_grams_per_s()
    }

    fn __repr__(&self) -> String {
        format!("MassFlow({:.6} kg/s)", self.inner.0)
    }

    fn __str__(&self) -> String {
        format!("{:.6} kg/s", self.inner.0)
    }

    fn __float__(&self) -> f64 {
        self.inner.0
    }

    fn __eq__(&self, other: &PyMassFlow) -> bool {
        (self.inner.0 - other.inner.0).abs() < 1e-15
    }

    fn __add__(&self, other: &PyMassFlow) -> PyMassFlow {
        PyMassFlow {
            inner: entropyk::MassFlow(self.inner.0 + other.inner.0),
        }
    }

    fn __sub__(&self, other: &PyMassFlow) -> PyMassFlow {
        PyMassFlow {
            inner: entropyk::MassFlow(self.inner.0 - other.inner.0),
        }
    }
}

