/**
 * Auth Domain - Form Utilities
 *
 * Business logic for password validation and strength checking.
 * UI configurations (labels, placeholders) belong in the frontend.
 */

// ============================================================================
// Password Requirements (Business Logic)
// ============================================================================

/**
 * Password requirements - single source of truth for validation rules.
 * Used by passwordSchema in common/schema.ts and for UI display.
 */
export const PASSWORD_REQUIREMENTS = [
  { key: "minLength", label: "At least 8 characters", regex: /.{8,}/ },
  { key: "uppercase", label: "One uppercase letter", regex: /[A-Z]/ },
  { key: "lowercase", label: "One lowercase letter", regex: /[a-z]/ },
  { key: "number", label: "One number", regex: /[0-9]/ },
] as const;

export type PasswordRequirementKey = (typeof PASSWORD_REQUIREMENTS)[number]["key"];

/**
 * Check password strength against requirements
 */
export function checkPasswordStrength(password: string): {
  requirements: Array<{ key: string; label: string; met: boolean }>;
  strength: number;
  isValid: boolean;
} {
  const requirements = PASSWORD_REQUIREMENTS.map(req => ({
    key: req.key,
    label: req.label,
    met: req.regex.test(password),
  }));

  const metCount = requirements.filter(r => r.met).length;
  const strength = (metCount / requirements.length) * 100;
  const isValid = metCount === requirements.length;

  return { requirements, strength, isValid };
}

/**
 * Get password strength display label and color class
 */
export function getPasswordStrengthDisplay(strength: number): {
  label: string;
  colorClass: string;
} {
  if (strength >= 100) return { label: "Strong", colorClass: "bg-green-500" };
  if (strength >= 80) return { label: "Good", colorClass: "bg-blue-500" };
  if (strength >= 60) return { label: "Fair", colorClass: "bg-yellow-500" };
  return { label: "Weak", colorClass: "bg-red-500" };
}
