use crate::impl_nutype_error_conversion;
use nutype::nutype;

/// Linha de endereço (máximo 255 caracteres)
///
/// # Validações
/// - Não pode ser vazio (após trim)
/// - Máximo 255 caracteres
///
/// # Exemplos
/// ```
/// use konduto::AddressLine;
///
/// let addr = AddressLine::try_new("123 Main Street, Apt 4B").unwrap();
/// assert_eq!(addr.as_str(), "123 Main Street, Apt 4B");
/// ```
#[nutype(
    sanitize(trim),
    validate(not_empty, len_char_max = 255),
    derive(
        Debug,
        Clone,
        PartialEq,
        Eq,
        Display,
        AsRef,
        Deref,
        Serialize,
        Deserialize,
    )
)]
pub struct AddressLine(String);

impl AddressLine {
    /// Retorna a linha de endereço como string slice (compatibilidade)
    pub fn as_str(&self) -> &str {
        self.as_ref()
    }
}

impl_nutype_error_conversion!(AddressLineError);

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

    #[test]
    fn test_valid_address() {
        let addr = AddressLine::try_new("123 Main Street, Apt 4B").unwrap();
        assert_eq!(addr.as_str(), "123 Main Street, Apt 4B");
    }

    #[test]
    fn test_address_with_whitespace() {
        let addr = AddressLine::try_new("  456 Oak Ave  ").unwrap();
        assert_eq!(addr.as_str(), "456 Oak Ave");
    }

    #[test]
    fn test_empty_address() {
        let result = AddressLine::try_new("");
        assert!(result.is_err());
    }

    #[test]
    fn test_address_too_long() {
        let long_addr = "a".repeat(256);
        let result = AddressLine::try_new(long_addr);
        assert!(result.is_err());
    }

    #[test]
    fn test_address_max_length() {
        let addr = "a".repeat(255);
        let result = AddressLine::try_new(addr);
        assert!(result.is_ok());
    }
}
