import { z } from '@hono/zod-openapi';

export const UserSchemaMt = 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个字符'
  }),
  phone: z.string().max(255, '手机号最多255个字符').nullable().openapi({
    example: '13800138000',
    description: '手机号'
  }),
  email: z.string().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: '头像文件信息'
  }),
  openid: z.string().max(255).nullable().optional().openapi({
    example: 'oABCDEFGH123456789',
    description: '微信小程序openid'
  }),
  unionid: z.string().max(255).nullable().optional().openapi({
    example: 'unionid123456789',
    description: '微信unionid'
  }),
  registrationSource: z.string().max(20).default('web').openapi({
    example: 'miniapp',
    description: '注册来源: web, miniapp'
  }),
  isDisabled: z.number().int().min(0).max(1).default(0).openapi({
    example: 0,
    description: '是否禁用(0:启用,1:禁用)'
  }),
  isDeleted: z.number().int().min(0).max(1).default(0).openapi({
    example: 0,
    description: '是否删除(0:未删除,1:已删除)'
  }),
  roles: z.array(z.object({
    id: z.number().int().positive().openapi({ description: '角色ID' }),
    name: z.string().max(255).openapi({ description: '角色名称', example: 'admin' }),
    description: z.string().max(255).nullable().openapi({ description: '角色描述', example: '管理员' }),
    permissions: z.array(z.string()).openapi({ description: '权限列表', example: ['user:create'] }),
    createdAt: z.coerce.date().openapi({ description: '创建时间' }),
    updatedAt: z.coerce.date().openapi({ description: '更新时间' })
  })).optional().openapi({
    example: [
      {
        id: 1,
        name: 'admin',
        description: '管理员',
        permissions: ['user:create'],
        createdAt: new Date(),
        updatedAt: new Date()
      }
    ],
    description: '用户角色列表'
  }),
  createdAt: z.coerce.date().openapi({ description: '创建时间' }),
  updatedAt: z.coerce.date().openapi({ description: '更新时间' })
});

export const FileSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '文件ID',
    example: 1
  }),
  name: z.string().max(255).openapi({
    description: '文件名称',
    example: '项目计划书.pdf'
  }),
  type: z.string().max(50).nullable().openapi({
    description: '文件类型',
    example: 'application/pdf'
  }),
  size: z.number().int().positive().nullable().openapi({
    description: '文件大小，单位字节',
    example: 102400
  }),
  path: z.string().max(512).openapi({
    description: '文件存储路径',
    example: '/uploads/documents/2023/project-plan.pdf'
  }),
  fullUrl: z.url().openapi({
    description: '完整文件访问URL',
    example: 'https://minio.example.com/d8dai/uploads/documents/2023/project-plan.pdf'
  }),
  description: z.string().nullable().openapi({
    description: '文件描述',
    example: '2023年度项目计划书'
  }),
  uploadUserId: z.number().int().positive().openapi({
    description: '上传用户ID',
    example: 1
  }),
  uploadUser: UserSchemaMt,
  uploadTime: z.coerce.date().openapi({
    description: '上传时间',
    example: '2023-01-15T10:30:00Z'
  }),
  lastUpdated: z.date().nullable().openapi({
    description: '最后更新时间',
    example: '2023-01-16T14:20:00Z'
  }),
  createdAt: z.coerce.date().openapi({
    description: '创建时间',
    example: '2023-01-15T10:30:00Z'
  }),
  updatedAt: z.coerce.date().openapi({
    description: '更新时间',
    example: '2023-01-16T14:20:00Z'
  })
});

export const CreateFileDto = z.object({
  name: z.string().min(1).max(255).openapi({
    description: '文件名称',
    example: '项目计划书.pdf'
  }),
  type: z.string().max(50).nullable().optional().openapi({
    description: '文件类型',
    example: 'application/pdf'
  }),
  size: z.coerce.number().int().positive().nullable().optional().openapi({
    description: '文件大小，单位字节',
    example: 102400
  }),
  path: z.string().max(512).openapi({
    description: '文件存储路径',
    example: '/uploads/documents/2023/project-plan.pdf'
  }),
  description: z.string().nullable().optional().openapi({
    description: '文件描述',
    example: '2023年度项目计划书'
  }),
  lastUpdated: z.coerce.date().nullable().optional().openapi({
    description: '最后更新时间',
    example: '2023-01-16T14:20:00Z'
  })
});

export const UpdateFileDto = z.object({
  name: z.string().max(255).optional().openapi({
    description: '文件名称',
    example: '项目计划书_v2.pdf'
  }),
  description: z.string().nullable().optional().openapi({
    description: '文件描述',
    example: '2023年度项目计划书（修订版）'
  })
});