export interface PasswordValidationResult {
  valid: boolean
  errors: string[]
}

const SPECIAL_CHARS = '!@#$%^&*'

export function validatePassword(password: string): PasswordValidationResult {
  const errors: string[] = []
  if (password.length < 8) errors.push('Mindestens 8 Zeichen erforderlich')
  if (!/[A-Z]/.test(password)) errors.push('Mindestens ein Großbuchstabe erforderlich')
  if (!/[a-z]/.test(password)) errors.push('Mindestens ein Kleinbuchstabe erforderlich')
  if (!/[0-9]/.test(password)) errors.push('Mindestens eine Zahl erforderlich')
  if (!new RegExp(`[${SPECIAL_CHARS.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}]`).test(password)) {
    errors.push(`Mindestens ein Sonderzeichen (${SPECIAL_CHARS}) erforderlich`)
  }
  return { valid: errors.length === 0, errors }
}

export function getPasswordStrength(password: string): 'weak' | 'medium' | 'strong' {
  const { errors } = validatePassword(password)
  const score = 5 - errors.length  // 0–5
  if (score <= 2) return 'weak'
  if (score <= 4) return 'medium'
  return 'strong'
}
