// Password validation rules configuration
export interface PasswordRule {
  id: number;
  title: string;
  regex?: RegExp;
  minLength?: number;
  validator: (password: string) => boolean;
  errorMessage: string;
}

export const passwordRules: PasswordRule[] = [
  {
    id: 1,
    title: 'At least 8 characters in length',
    minLength: 8,
    validator: (password: string) => password.length >= 8,
    errorMessage: 'Password must be at least 8 characters long',
  },
  {
    id: 2,
    title: 'At least one uppercase letter',
    regex: /[A-Z]/,
    validator: (password: string) => /[A-Z]/.test(password),
    errorMessage: 'Password must contain at least one uppercase letter',
  },
  {
    id: 3,
    title: 'At least one lowercase letter',
    regex: /[a-z]/,
    validator: (password: string) => /[a-z]/.test(password),
    errorMessage: 'Password must contain at least one lowercase letter',
  },
  {
    id: 4,
    title: 'At least one number',
    regex: /[0-9]/,
    validator: (password: string) => /[0-9]/.test(password),
    errorMessage: 'Password must contain at least one number',
  },
  {
    id: 5,
    title: 'At least one symbol',
    regex: /[!@#$%^&*(),.?":{}|<>]/,
    validator: (password: string) => /[!@#$%^&*(),.?":{}|<>]/.test(password),
    errorMessage: 'Password must contain at least one symbol',
  },
];

// Helper function to validate password against all rules
export const validatePassword = (password: string) => {
  return passwordRules.map(rule => ({
    ...rule,
    isFine: rule.validator(password),
  }));
};

// Helper function to get the first failing rule's error message
export const getPasswordError = (password: string): string | null => {
  const failingRule = passwordRules.find(rule => !rule.validator(password));
  return failingRule ? failingRule.errorMessage : null;
};