/**
 * 登录表单验证规则
 */

import type { FormItemRule } from 'element-plus'

/**
 * 用户名验证规则
 */
export const usernameRules = (t: (key: string) => string): FormItemRule[] => [
  {
    required: true,
    message: t('login.placeholder[0]'),
    trigger: 'blur'
  },
  {
    min: 2,
    max: 20,
    message: t('login.validation.usernameLength'),
    trigger: 'blur'
  },
  {
    pattern: /^[a-zA-Z0-9_]+$/,
    message: t('login.validation.usernamePattern'),
    trigger: 'blur'
  }
]

/**
 * 密码验证规则
 */
export const passwordRules = (t: (key: string) => string): FormItemRule[] => [
  {
    required: true,
    message: t('login.placeholder[1]'),
    trigger: 'blur'
  },
  {
    min: 6,
    max: 20,
    message: t('login.validation.passwordLength'),
    trigger: 'blur'
  }
]

/**
 * 强密码验证规则（用于注册/修改密码）
 */
export const strongPasswordRules = (t: (key: string) => string): FormItemRule[] => [
  {
    required: true,
    message: t('login.placeholder[1]'),
    trigger: 'blur'
  },
  {
    min: 8,
    max: 20,
    message: t('login.validation.strongPasswordLength'),
    trigger: 'blur'
  },
  {
    validator: (rule: any, value: any, callback: any) => {
      if (!value) {
        callback()
        return
      }

      // 必须包含大小写字母、数字
      const hasUpperCase = /[A-Z]/.test(value)
      const hasLowerCase = /[a-z]/.test(value)
      const hasNumber = /\d/.test(value)

      if (!hasUpperCase || !hasLowerCase || !hasNumber) {
        callback(new Error(t('login.validation.strongPasswordPattern')))
      } else {
        callback()
      }
    },
    trigger: 'blur'
  }
]

/**
 * 确认密码验证规则
 */
export const confirmPasswordRules = (
  t: (key: string) => string,
  getPassword: () => string
): FormItemRule[] => [
  {
    required: true,
    message: t('login.validation.confirmPasswordRequired'),
    trigger: 'blur'
  },
  {
    validator: (rule: any, value: any, callback: any) => {
      if (!value) {
        callback()
        return
      }

      if (value !== getPassword()) {
        callback(new Error(t('login.validation.confirmPasswordNotMatch')))
      } else {
        callback()
      }
    },
    trigger: 'blur'
  }
]

/**
 * 手机号验证规则
 */
export const phoneRules = (t: (key: string) => string, required = false): FormItemRule[] => {
  const rules: FormItemRule[] = []

  if (required) {
    rules.push({
      required: true,
      message: t('login.validation.phoneRequired'),
      trigger: 'blur'
    })
  }

  rules.push({
    pattern: /^1[3-9]\d{9}$/,
    message: t('login.validation.phonePattern'),
    trigger: 'blur'
  })

  return rules
}

/**
 * 邮箱验证规则
 */
export const emailRules = (t: (key: string) => string, required = false): FormItemRule[] => {
  const rules: FormItemRule[] = []

  if (required) {
    rules.push({
      required: true,
      message: t('login.validation.emailRequired'),
      trigger: 'blur'
    })
  }

  rules.push({
    type: 'email',
    message: t('login.validation.emailPattern'),
    trigger: 'blur'
  })

  return rules
}

/**
 * 验证码验证规则
 */
export const captchaRules = (t: (key: string) => string): FormItemRule[] => [
  {
    required: true,
    message: t('login.validation.captchaRequired'),
    trigger: 'blur'
  },
  {
    len: 4,
    message: t('login.validation.captchaLength'),
    trigger: 'blur'
  }
]
