//! Constraint types and the `constraint!` macro

use super::expression::Expression;

/// Sense (direction) of a linear constraint.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConstraintSense {
    /// Less-than-or-equal: `lhs <= rhs`
    Le,
    /// Greater-than-or-equal: `lhs >= rhs`
    Ge,
    /// Equality: `lhs == rhs`
    Eq,
}

/// A linear constraint: `lhs sense rhs`.
///
/// Stored in normalized form: `lhs` contains only variable terms (no constant),
/// and `rhs` is a scalar.
#[derive(Debug, Clone)]
pub struct Constraint {
    /// Left-hand side (variable terms only, constant == 0)
    pub(crate) lhs: Expression,
    /// Right-hand side scalar
    pub(crate) rhs: f64,
    /// Constraint direction
    pub(crate) sense: ConstraintSense,
}

/// Build a `Constraint` using inequality syntax: `constraint!(x <= 5.0)` for a
/// single variable, `constraint!((expr) <= rhs)` for a parenthesised expression.
///
/// ```
/// use otspot_model::{Model, constraint};
/// let mut model = Model::new("demo");
/// let x = model.add_var("x", 0.0, f64::INFINITY);
/// let y = model.add_var("y", 0.0, 10.0);
/// model.add_constraint(constraint!((2.0 * x + 3.0 * y) <= 12.0));
/// model.add_constraint(constraint!((x + y) >= 3.0));
/// ```
#[macro_export]
macro_rules! constraint {
    // Complex LHS in parentheses
    (($lhs:expr) <= $rhs:expr) => {
        $crate::expression::Expression::from($lhs).leq($rhs)
    };
    (($lhs:expr) >= $rhs:expr) => {
        $crate::expression::Expression::from($lhs).geq($rhs)
    };
    (($lhs:expr) == $rhs:expr) => {
        $crate::expression::Expression::from($lhs).eq_constraint($rhs)
    };
    // Single variable (ident can be followed by <= in macro rules)
    ($lhs:ident <= $rhs:expr) => {
        $crate::expression::Expression::from($lhs).leq($rhs)
    };
    ($lhs:ident >= $rhs:expr) => {
        $crate::expression::Expression::from($lhs).geq($rhs)
    };
    ($lhs:ident == $rhs:expr) => {
        $crate::expression::Expression::from($lhs).eq_constraint($rhs)
    };
}
