import { z } from "zod"

export const SendMessageSchema = z
  .object({
    receiverId: z.string().trim().min(1),
    subject: z.string().trim().max(255).optional().nullable(),
    content: z.string().trim().min(1),
    parentMessageId: z.string().trim().optional().nullable(),
  })
  .transform((v) => ({
    receiverId: v.receiverId,
    subject: v.subject && v.subject.length > 0 ? v.subject : null,
    content: v.content,
    parentMessageId:
      v.parentMessageId && v.parentMessageId.length > 0 ? v.parentMessageId : null,
  }))

export type SendMessageInput = z.infer<typeof SendMessageSchema>

/** 校验单个 messageId / notificationId 路径参数 */
export const MessageIdSchema = z.object({
  messageId: z.string().trim().min(1),
})

export type MessageIdInput = z.infer<typeof MessageIdSchema>

/** 校验通知偏好更新表单（8 个布尔字段 + 免打扰时段，来自 checkbox/FormData） */
export const UpdateNotificationPreferencesSchema = z.object({
  emailEnabled: z.boolean(),
  smsEnabled: z.boolean(),
  pushEnabled: z.boolean(),
  homeworkNotifications: z.boolean(),
  gradeNotifications: z.boolean(),
  announcementNotifications: z.boolean(),
  messageNotifications: z.boolean(),
  attendanceNotifications: z.boolean(),
  quietHoursEnabled: z.boolean(),
  quietHoursStart: z.string().trim().regex(/^([01]\d|2[0-3]):[0-5]\d$/, "Invalid time format").nullable().optional(),
  quietHoursEnd: z.string().trim().regex(/^([01]\d|2[0-3]):[0-5]\d$/, "Invalid time format").nullable().optional(),
})

export type UpdateNotificationPreferencesFormInput = z.infer<
  typeof UpdateNotificationPreferencesSchema
>
