/**
 * Validation utility functions
 */

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

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

/**
 * Validate phone number (basic international format)
 */
export function isValidPhone(phone: string): boolean {
	// Allows: +49123456789, 0123456789, +1 (555) 123-4567
	const phoneRegex = /^[+]?[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/;
	return phoneRegex.test(phone) && phone.replace(/\D/g, '').length >= 6;
}

/**
 * Validate password strength
 * Returns an object with validation details
 */
export function validatePassword(password: string): {
	isValid: boolean;
	hasMinLength: boolean;
	hasUppercase: boolean;
	hasLowercase: boolean;
	hasNumber: boolean;
	hasSpecialChar: boolean;
	score: number;
} {
	const hasMinLength = password.length >= 8;
	const hasUppercase = /[A-Z]/.test(password);
	const hasLowercase = /[a-z]/.test(password);
	const hasNumber = /[0-9]/.test(password);
	const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);

	const score = [hasMinLength, hasUppercase, hasLowercase, hasNumber, hasSpecialChar].filter(
		Boolean
	).length;

	return {
		isValid: hasMinLength && hasUppercase && hasLowercase && hasNumber,
		hasMinLength,
		hasUppercase,
		hasLowercase,
		hasNumber,
		hasSpecialChar,
		score,
	};
}

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

/**
 * Check if a string exceeds max length
 */
export function isWithinMaxLength(value: string, maxLength: number): boolean {
	return value.length <= maxLength;
}

/**
 * Check if a string meets minimum length
 */
export function meetsMinLength(value: string, minLength: number): boolean {
	return value.length >= minLength;
}

/**
 * Validate UUID format
 */
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);
}

/**
 * Validate hex color format
 */
export function isValidHexColor(color: string): boolean {
	const hexRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
	return hexRegex.test(color);
}
