import { z } from 'zod'

export const registerSchema = z.object({
  email: z
    .string()
    .min(1, '邮箱不能为空')
    .email('邮箱格式不正确')
    .max(255, '邮箱长度不能超过255个字符'),
  password: z
    .string()
    .min(8, '密码至少需要8个字符')
    .max(128, '密码长度不能超过128个字符')
    .regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/, '密码必须包含大小写字母和数字'),
  name: z
    .string()
    .min(1, '姓名不能为空')
    .max(100, '姓名长度不能超过100个字符')
    .trim(),
  terms: z
    .boolean()
    .refine(val => val === true, '必须同意服务条款')
})

export const loginSchema = z.object({
  email: z
    .string()
    .min(1, '邮箱不能为空')
    .email('邮箱格式不正确'),
  password: z
    .string()
    .min(1, '密码不能为空')
})

export const passwordResetRequestSchema = z.object({
  email: z
    .string()
    .min(1, '邮箱不能为空')
    .email('邮箱格式不正确')
})

export const passwordResetConfirmSchema = z.object({
  token: z
    .string()
    .min(1, '重置令牌不能为空'),
  newPassword: z
    .string()
    .min(8, '密码至少需要8个字符')
    .max(128, '密码长度不能超过128个字符')
    .regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/, '密码必须包含大小写字母和数字'),
  confirmPassword: z
    .string()
    .min(1, '确认密码不能为空')
}).refine(data => data.newPassword === data.confirmPassword, {
  message: '两次输入的密码不一致',
  path: ['confirmPassword']
})

export const profileUpdateSchema = z.object({
  name: z
    .string()
    .min(1, '姓名不能为空')
    .max(100, '姓名长度不能超过100个字符')
    .trim()
    .optional(),
  avatar: z
    .string()
    .url('头像必须是有效的URL')
    .optional()
})

export const changePasswordSchema = z.object({
  currentPassword: z
    .string()
    .min(1, '当前密码不能为空'),
  newPassword: z
    .string()
    .min(8, '新密码至少需要8个字符')
    .max(128, '新密码长度不能超过128个字符')
    .regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/, '新密码必须包含大小写字母和数字'),
  confirmPassword: z
    .string()
    .min(1, '确认密码不能为空')
}).refine(data => data.newPassword === data.confirmPassword, {
  message: '两次输入的密码不一致',
  path: ['confirmPassword']
}).refine(data => data.currentPassword !== data.newPassword, {
  message: '新密码不能与当前密码相同',
  path: ['newPassword']
})

export const refreshTokenSchema = z.object({
  refreshToken: z
    .string()
    .min(1, '刷新令牌不能为空')
})