const STRONG_PASSWORD_REGEX =
  /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&#^()[\]{}\-_=+|;:'",.<>/\\`~])[A-Za-z\d@$!%*?&#^()[\]{}\-_=+|;:'",.<>/\\`~]{8,}$/;

const PASSWORD_RULES = [
  'At least 8 characters',
  'At least one uppercase letter (A-Z)',
  'At least one lowercase letter (a-z)',
  'At least one number (0-9)',
  'At least one special character (@$!%*?&# etc.)',
];

function isStrongPassword(password) {
  return typeof password === 'string' && STRONG_PASSWORD_REGEX.test(password);
}

function getPasswordValidationMessage(password) {
  if (!password || typeof password !== 'string') {
    return 'Password is required';
  }
  if (password.length < 8) return 'Password must be at least 8 characters';
  if (!/[a-z]/.test(password)) return 'Password must contain a lowercase letter';
  if (!/[A-Z]/.test(password)) return 'Password must contain an uppercase letter';
  if (!/\d/.test(password)) return 'Password must contain a number';
  if (!/[@$!%*?&#^()[\]{}\-_=+|;:'",.<>/\\`~]/.test(password)) {
    return 'Password must contain a special character';
  }
  return null;
}

module.exports = {
  STRONG_PASSWORD_REGEX,
  PASSWORD_RULES,
  isStrongPassword,
  getPasswordValidationMessage,
};
