import { z } from '@hono/zod-openapi';
import { UserSchema } from '@/server/modules/users/user.schema';
import { UserType } from '@/server/modules/users/user.enum';

// 基础消息Schema
export const ChatMessageSchema = z.object({
  id: z.number().int().positive().openapi({
    example: 1,
    description: '消息ID'
  }),
  classId: z.string().openapi({
    example: 'class_123456',
    description: '课堂ID'
  }),
  type: z.enum(['text', 'image', 'system']).openapi({
    example: 'text',
    description: '消息类型'
  }),
  content: z.string().openapi({
    example: '这是一条文本消息',
    description: '消息内容'
  }),
  senderId: z.number().int().positive().nullable().openapi({
    example: 1,
    description: '发送者ID'
  }),
  sender: UserSchema.omit({ password: true }).nullable().optional().openapi({
    description: '发送者用户信息'
  }),
  senderName: z.string().nullable().openapi({
    example: '张三',
    description: '发送者名称'
  }),
  senderType: z.enum([UserType.TEACHER, UserType.STUDENT, UserType.TRAINEE]).nullable().openapi({
    example: UserType.TEACHER,
    description: '发送者用户类型（teacher/student/trainee）'
  }),
  timestamp: z.coerce.number<number>().int().positive().openapi({
    example: 1704067200000,
    description: '消息时间戳'
  }),
  fileId: z.number().int().positive().nullable().openapi({
    example: 1,
    description: '关联文件ID（用于图片消息）'
  }),
  file: z.object({
    id: z.number().int().positive().openapi({ description: '文件ID' }),
    name: z.string().max(255).openapi({ description: '文件名', example: 'example.jpg' }),
    fullUrl: z.string().openapi({ description: '文件完整URL', example: 'https://example.com/file.jpg' }),
    type: z.string().nullable().openapi({ description: '文件类型', example: 'image/jpeg' }),
    size: z.number().nullable().openapi({ description: '文件大小(字节)', example: 102400 })
  }).nullable().optional().openapi({
    description: '{描述}文件信息'
  }),
  createdBy: z.number().int().positive().nullable().openapi({
    example: 1,
    description: '创建用户ID'
  }),
  updatedBy: z.number().int().positive().nullable().openapi({
    example: 1,
    description: '更新用户ID'
  }),
  createdAt: z.coerce.date().openapi({
    example: '2024-01-01T12:00:00Z',
    description: '创建时间'
  }),
  updatedAt: z.coerce.date().openapi({
    example: '2024-01-01T12:00:00Z',
    description: '更新时间'
  })
});

// 创建消息DTO
export const CreateChatMessageDto = z.object({
  classId: z.string().min(1, '课堂ID不能为空').openapi({
    example: 'class_123456',
    description: '课堂ID'
  }),
  type: z.enum(['text', 'image', 'system']).openapi({
    example: 'text',
    description: '消息类型'
  }),
  content: z.string().min(1, '消息内容不能为空').openapi({
    example: '这是一条文本消息',
    description: '消息内容'
  }),
  senderId: z.coerce.number().int().positive().nullable().optional().openapi({
    example: 1,
    description: '发送者ID'
  }),
  senderName: z.string().nullable().optional().openapi({
    example: '张三',
    description: '发送者名称'
  }),
  senderType: z.enum([UserType.TEACHER, UserType.STUDENT, UserType.TRAINEE]).nullable().optional().openapi({
    example: UserType.TEACHER,
    description: '发送者用户类型（teacher/student/trainee）'
  }),
  timestamp: z.coerce.number().int().positive().openapi({
    example: 1704067200000,
    description: '消息时间戳（毫秒）'
  }),
  fileId: z.coerce.number().int().positive().nullable().optional().openapi({
    example: 1,
    description: '关联文件ID（用于图片消息）'
  })
});

// 更新消息DTO
export const UpdateChatMessageDto = z.object({
  classId: z.string().min(1, '课堂ID不能为空').optional().openapi({
    example: 'class_123456',
    description: '课堂ID'
  }),
  type: z.enum(['text', 'image', 'system']).optional().openapi({
    example: 'text',
    description: '消息类型'
  }),
  content: z.string().min(1, '消息内容不能为空').optional().openapi({
    example: '这是一条文本消息',
    description: '消息内容'
  }),
  senderId: z.coerce.number().int().positive().nullable().optional().openapi({
    example: 1,
    description: '发送者ID'
  }),
  senderName: z.string().nullable().optional().openapi({
    example: '张三',
    description: '发送者名称'
  }),
  senderType: z.enum([UserType.TEACHER, UserType.STUDENT, UserType.TRAINEE]).nullable().optional().openapi({
    example: UserType.TEACHER,
    description: '发送者用户类型（teacher/student/trainee）'
  }),
  timestamp: z.coerce.number().int().positive().optional().openapi({
    example: 1704067200000,
    description: '消息时间戳'
  }),
  fileId: z.coerce.number().int().positive().nullable().optional().openapi({
    example: 1,
    description: '关联文件ID（用于图片消息）'
  })
});

// 消息列表响应Schema
export const ChatMessageListResponse = z.object({
  data: z.array(ChatMessageSchema),
  pagination: z.object({
    total: z.number().openapi({
      example: 100,
      description: '总记录数'
    }),
    current: z.number().openapi({
      example: 1,
      description: '当前页码'
    }),
    pageSize: z.number().openapi({
      example: 10,
      description: '每页数量'
    })
  })
});

// 历史消息查询参数
export const HistoryQuerySchema = z.object({
  classId: z.string().min(1, '课堂ID不能为空').openapi({
    example: 'class_123456',
    description: '课堂ID'
  }),
  page: z.coerce.number().int().positive().default(1).openapi({
    example: 1,
    description: '页码'
  }),
  pageSize: z.coerce.number().int().positive().default(50).openapi({
    example: 50,
    description: '每页数量'
  })
});