/**
 * Validation utility functions
 */

/**
 * Validate Chinese phone number
 */
export function isValidChinesePhone(phone: string): boolean {
  const phoneRegex = /^1[3-9]\d{9}$/;
  return phoneRegex.test(phone);
}

/**
 * Validate Hong Kong phone number
 */
export function isValidHKPhone(phone: string): boolean {
  const phoneRegex = /^[2-9]\d{7}$/;
  return phoneRegex.test(phone);
}

/**
 * Validate phone number (supports both mainland China and HK)
 */
export function isValidPhone(phone: string): boolean {
  const cleanPhone = phone.replace(/[\s-]/g, '');
  return isValidChinesePhone(cleanPhone) || isValidHKPhone(cleanPhone);
}

/**
 * Validate email address
 */
export function isValidEmail(email: string): boolean {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
}

/**
 * Validate UUID
 */
export function isValidUUID(uuid: string): boolean {
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
  return uuidRegex.test(uuid);
}

/**
 * Check if string is empty or only whitespace
 */
export function isEmpty(str: string | null | undefined): boolean {
  return str === null || str === undefined || str.trim().length === 0;
}

/**
 * Validate password strength
 * At least 8 characters, 1 uppercase, 1 lowercase, 1 number
 */
export function isStrongPassword(password: string): boolean {
  const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/;
  return passwordRegex.test(password);
}

/**
 * Validate URL
 */
export function isValidUrl(url: string): boolean {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
}

/**
 * Validate that a number is within range
 */
export function isInRange(value: number, min: number, max: number): boolean {
  return value >= min && value <= max;
}

/**
 * Validate age for immigration purposes (typically 18-60)
 */
export function isValidImmigrationAge(age: number): boolean {
  return isInRange(age, 18, 60);
}
