import { z } from '@hono/zod-openapi';
import { DeleteStatus, DisabledStatus } from '@/share/types';
import { RoleSchema } from './role.schema';
// import { FileSchema } from '@/server/modules/files/file.schema';
import { UserType } from './user.enum';

// 基础用户 schema（包含所有字段）
export const UserSchema = z.object({
  id: z.number().int().positive().openapi({ description: '用户ID' }),
  username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
    example: 'admin',
    description: '用户名，3-255个字符'
  }),
  password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
    example: 'password123',
    description: '密码，最少6位'
  }),
  phone: z.string().max(255, '手机号最多255个字符').nullable().openapi({
    example: '13800138000',
    description: '手机号'
  }),
  email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().openapi({
    example: 'user@example.com',
    description: '邮箱'
  }),
  nickname: z.string().max(255, '昵称最多255个字符').nullable().openapi({
    example: '昵称',
    description: '用户昵称'
  }),
  name: z.string().max(255, '姓名最多255个字符').nullable().openapi({
    example: '张三',
    description: '真实姓名'
  }),
  avatarFileId: z.number().int().positive().nullable().openapi({
    example: 1,
    description: '头像文件ID'
  }),
  avatarFile: z.object({
    id: z.number().int().positive().openapi({ description: '文件ID' }),
    name: z.string().max(255).openapi({ description: '文件名', example: 'avatar.jpg' }),
    fullUrl: z.string().openapi({ description: '文件完整URL', example: 'https://example.com/avatar.jpg' }),
    type: z.string().nullable().openapi({ description: '文件类型', example: 'image/jpeg' }),
    size: z.number().nullable().openapi({ description: '文件大小(字节)', example: 102400 })
  }).nullable().optional().openapi({
    description: '头像文件信息'
  }),
  isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED).openapi({
    example: DisabledStatus.ENABLED,
    description: '是否禁用(0:启用,1:禁用)'
  }),
  isDeleted: z.number().int().min(0).max(1).default(DeleteStatus.NOT_DELETED).openapi({
    example: DeleteStatus.NOT_DELETED,
    description: '是否删除(0:未删除,1:已删除)'
  }),
  roles: z.array(RoleSchema).optional().openapi({
    example: [
      {
        id: 1,
        name: 'admin',
        description: '管理员',
        permissions: ['user:create'],
        createdAt: new Date(),
        updatedAt: new Date()
      }
    ],
    description: '用户角色列表'
  }),
  userType: z.enum([UserType.TEACHER, UserType.STUDENT, UserType.TRAINEE]).default(UserType.STUDENT).openapi({
    example: UserType.STUDENT,
    description: '用户类型(teacher:老师,student:学生,trainee:学员)'
  }),
  wechatOpenid: z.string().max(255, '微信openid最多255个字符').nullable().openapi({
    example: 'o6_bmjrPTlm6_2sgVt7hMZOPfL2M',
    description: '微信开放平台openid'
  }),
  wechatUnionid: z.string().max(255, '微信unionid最多255个字符').nullable().openapi({
    example: 'o6_bmasdasdsad6_2sgVt7hMZOPfL2M',
    description: '微信开放平台unionid'
  }),
  wechatNickname: z.string().max(255, '微信昵称最多255个字符').nullable().openapi({
    example: '微信用户',
    description: '微信昵称'
  }),
  wechatAvatar: z.string().max(500, '微信头像URL最多500个字符').nullable().openapi({
    example: 'http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46',
    description: '微信头像URL'
  }),
  wechatSex: z.number().int('微信性别必须是整数').min(0, '微信性别最小值为0').max(2, '微信性别最大值为2').nullable().openapi({
    example: 1,
    description: '微信性别(1:男,2:女,0:未知)'
  }),
  wechatProvince: z.string().max(100, '微信省份最多100个字符').nullable().openapi({
    example: '广东省',
    description: '微信省份'
  }),
  wechatCity: z.string().max(100, '微信城市最多100个字符').nullable().openapi({
    example: '深圳市',
    description: '微信城市'
  }),
  wechatCountry: z.string().max(100, '微信国家最多100个字符').nullable().openapi({
    example: '中国',
    description: '微信国家'
  }),
  traineeValidFrom: z.coerce.date().nullable().openapi({
    example: '2024-01-01T00:00:00Z',
    description: '学员有效期开始时间'
  }),
  traineeValidTo: z.coerce.date().nullable().openapi({
    example: '2024-12-31T23:59:59Z',
    description: '学员有效期结束时间'
  }),
  createdAt: z.coerce.date().openapi({ description: '创建时间' }),
  updatedAt: z.coerce.date().openapi({ description: '更新时间' })
});

// 创建用户请求 schema
export const CreateUserDto = z.object({
  username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
    example: 'admin',
    description: '用户名，3-255个字符'
  }),
  password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
    example: 'password123',
    description: '密码，最少6位'
  }),
  phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
    example: '13800138000',
    description: '手机号'
  }),
  email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
    example: 'user@example.com',
    description: '邮箱'
  }),
  nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
    example: '昵称',
    description: '用户昵称'
  }),
  name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
    example: '张三',
    description: '真实姓名'
  }),
  avatarFileId: z.number().int().positive().nullable().optional().openapi({
    example: 1,
    description: '头像文件ID'
  }),
  userType: z.enum([UserType.TEACHER, UserType.STUDENT, UserType.TRAINEE]).default(UserType.STUDENT).openapi({
    example: UserType.STUDENT,
    description: '用户类型(teacher:老师,student:学生,trainee:学员)'
  }),
  isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').default(DisabledStatus.ENABLED).optional().openapi({
    example: DisabledStatus.ENABLED,
    description: '是否禁用(0:启用,1:禁用)'
  }),
  traineeValidFrom: z.coerce.date().nullable().optional().openapi({
    example: '2024-01-01T00:00:00Z',
    description: '学员有效期开始时间'
  }),
  traineeValidTo: z.coerce.date().nullable().optional().openapi({
    example: '2024-12-31T23:59:59Z',
    description: '学员有效期结束时间'
  }),
});

// 更新用户请求 schema
export const UpdateUserDto = z.object({
  username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').optional().openapi({
    example: 'admin',
    description: '用户名，3-255个字符'
  }),
  password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').optional().openapi({
    example: 'password123',
    description: '密码，最少6位'
  }),
  phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
    example: '13800138000',
    description: '手机号'
  }),
  email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
    example: 'user@example.com',
    description: '邮箱'
  }),
  nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
    example: '昵称',
    description: '用户昵称'
  }),
  name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
    example: '张三',
    description: '真实姓名'
  }),
  avatarFileId: z.number().int().positive().nullable().optional().openapi({
    example: 1,
    description: '头像文件ID'
  }),
  isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').optional().openapi({
    example: DisabledStatus.ENABLED,
    description: '是否禁用(0:启用,1:禁用)'
  }),
  userType: z.enum([UserType.TEACHER, UserType.STUDENT, UserType.TRAINEE]).optional().openapi({
    example: UserType.STUDENT,
    description: '用户类型(teacher:老师,student:学生,trainee:学员)'
  }),
  traineeValidFrom: z.coerce.date().nullable().optional().openapi({
    example: '2024-01-01T00:00:00Z',
    description: '学员有效期开始时间'
  }),
  traineeValidTo: z.coerce.date().nullable().optional().openapi({
    example: '2024-12-31T23:59:59Z',
    description: '学员有效期结束时间'
  }),
});