/**
 * Validates and sanitizes a mobile number.
 * Returns sanitized number or null if invalid.
 */
function validateMobile(mobile) {
  if (!mobile || typeof mobile !== "string") return null;

  // Remove spaces, dashes, and parentheses
  const sanitized = mobile.replace(/[\s\-\(\)]/g, "");

  // Allow optional + prefix, then 10-15 digits
  const mobileRegex = /^\+?\d{10,15}$/;

  if (!mobileRegex.test(sanitized)) return null;

  return sanitized;
}

/**
 * Sanitizes a string input - trims and limits length.
 */
function sanitizeString(str, maxLength = 100) {
  if (!str || typeof str !== "string") return null;
  return str.trim().slice(0, maxLength);
}

/**
 * Validates the name field.
 */
function validateName(name) {
  const sanitized = sanitizeString(name, 100);
  if (!sanitized || sanitized.length < 1) return null;
  return sanitized;
}

module.exports = { validateMobile, sanitizeString, validateName };
