use crate::types::validation_errors::ValidationError;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Quantidade do produto
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(transparent)]
pub struct Quantity(u32);

impl Quantity {
    /// Cria uma nova quantidade validada
    pub fn new(value: u32) -> Result<Self, ValidationError> {
        if value == 0 {
            return Err(ValidationError::InvalidFormat {
                field: "quantity".to_string(),
                message: "quantity must be greater than 0".to_string(),
            });
        }

        Ok(Self(value))
    }

    pub fn value(&self) -> u32 {
        self.0
    }
}

impl fmt::Display for Quantity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quantity_valid() {
        let qty = Quantity::new(1).unwrap();
        assert_eq!(qty.value(), 1);
    }

    #[test]
    fn test_quantity_zero() {
        let result = Quantity::new(0);
        assert!(result.is_err());
    }

    #[test]
    fn test_quantity_large() {
        let qty = Quantity::new(999999).unwrap();
        assert_eq!(qty.value(), 999999);
    }

    #[test]
    fn test_quantity_display() {
        let qty = Quantity::new(5).unwrap();
        assert_eq!(format!("{}", qty), "5");
    }
}

