//! Form validation utilities
//!
//! Provides common validation functions for form inputs.

/// Validates that a field is not empty
///
/// # Examples
///
/// ```
/// use vapora_leptos_ui::validate_required;
///
/// assert!(validate_required("hello", "Name").is_ok());
/// assert!(validate_required("", "Name").is_err());
/// assert!(validate_required("   ", "Name").is_err());
/// ```
pub fn validate_required(value: &str, field_name: &str) -> Result<(), String> {
    if value.trim().is_empty() {
        Err(format!("{} is required", field_name))
    } else {
        Ok(())
    }
}

/// Validates email format (basic check)
///
/// # Examples
///
/// ```
/// use vapora_leptos_ui::validate_email;
///
/// assert!(validate_email("user@example.com").is_ok());
/// assert!(validate_email("invalid").is_err());
/// assert!(validate_email("@example.com").is_err());
/// ```
pub fn validate_email(value: &str) -> Result<(), String> {
    // Basic email validation: must have text before @, and domain with .
    if let Some(at_pos) = value.find('@') {
        if at_pos == 0 {
            return Err("Invalid email format".to_string());
        }
        let domain = &value[at_pos + 1..];
        if domain.is_empty()
            || !domain.contains('.')
            || domain.starts_with('.')
            || domain.ends_with('.')
        {
            return Err("Invalid email format".to_string());
        }
        Ok(())
    } else {
        Err("Invalid email format".to_string())
    }
}

/// Validates minimum length
///
/// # Examples
///
/// ```
/// use vapora_leptos_ui::validate_min_length;
///
/// assert!(validate_min_length("password123", 8, "Password").is_ok());
/// assert!(validate_min_length("short", 8, "Password").is_err());
/// ```
pub fn validate_min_length(value: &str, min: usize, field_name: &str) -> Result<(), String> {
    if value.len() < min {
        Err(format!(
            "{} must be at least {} characters",
            field_name, min
        ))
    } else {
        Ok(())
    }
}

/// Validates maximum length
///
/// # Examples
///
/// ```
/// use vapora_leptos_ui::validate_max_length;
///
/// assert!(validate_max_length("hello", 10, "Username").is_ok());
/// assert!(validate_max_length("verylongusername", 10, "Username").is_err());
/// ```
pub fn validate_max_length(value: &str, max: usize, field_name: &str) -> Result<(), String> {
    if value.len() > max {
        Err(format!("{} must be at most {} characters", field_name, max))
    } else {
        Ok(())
    }
}

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

    #[test]
    fn test_validate_required() {
        assert!(validate_required("value", "Field").is_ok());
        assert!(validate_required("", "Field").is_err());
        assert!(validate_required("   ", "Field").is_err());
    }

    #[test]
    fn test_validate_email() {
        assert!(validate_email("user@example.com").is_ok());
        assert!(validate_email("invalid").is_err());
        assert!(validate_email("@example.com").is_err());
        assert!(validate_email("user@").is_err());
    }

    #[test]
    fn test_validate_min_length() {
        assert!(validate_min_length("password", 8, "Password").is_ok());
        assert!(validate_min_length("short", 8, "Password").is_err());
    }

    #[test]
    fn test_validate_max_length() {
        assert!(validate_max_length("hello", 10, "Username").is_ok());
        assert!(validate_max_length("verylongusername", 10, "Username").is_err());
    }
}
