import { z } from "zod";

/**
 * Templates 模块 DTO + Zod 校验。
 *
 * 对齐 proto msg.proto NotificationTemplate 字段。
 */

const typeEnum = z.enum([
  "system",
  "exam",
  "homework",
  "grade",
  "attendance",
  "mastery",
]);
const channelEnum = z.enum(["in_app", "email", "sms", "push", "wechat"]);
const statusEnum = z.enum(["draft", "active", "archived"]);

export const createTemplateSchema = z.object({
  code: z.string().min(1).max(64),
  type: typeEnum,
  titleTemplate: z.string().min(1).max(255),
  contentTemplate: z.string().min(1),
  defaultChannels: z.array(channelEnum).min(1),
  variables: z.array(z.string()).default([]),
  locale: z.string().max(16).default("zh-CN"),
});

export type CreateTemplateDto = z.infer<typeof createTemplateSchema>;

export const updateTemplateSchema = z.object({
  titleTemplate: z.string().min(1).max(255).optional(),
  contentTemplate: z.string().min(1).optional(),
  defaultChannels: z.array(channelEnum).min(1).optional(),
  variables: z.array(z.string()).optional(),
  status: statusEnum.optional(),
});

export type UpdateTemplateDto = z.infer<typeof updateTemplateSchema>;

export const listTemplatesSchema = z.object({
  type: typeEnum.optional(),
  status: statusEnum.optional(),
});

export type ListTemplatesDto = z.infer<typeof listTemplatesSchema>;

export const renderTemplateSchema = z.object({
  code: z.string().min(1).max(64),
  variables: z.record(z.string()).default({}),
  locale: z.string().max(16).optional(),
});

export type RenderTemplateDto = z.infer<typeof renderTemplateSchema>;
