import { z } from "zod";

const optionalText = (max: number, message: string) => z.string().trim().max(max, message).optional().default("");

export const menuFormSchema = z.object({
  code: z.string().trim().min(1, "请输入菜单编码").max(80, "菜单编码不能超过 80 个字符"),
  icon: optionalText(40, "图标不能超过 40 个字符"),
  label: z.string().trim().min(1, "请输入菜单名称").max(60, "菜单名称不能超过 60 个字符"),
  parentId: z
    .union([z.literal(""), z.coerce.number().int().positive(), z.null()])
    .optional()
    .transform((value) => (value === "" || value === undefined ? null : value)),
  path: optionalText(120, "路径不能超过 120 个字符"),
  permissionCode: optionalText(120, "权限码不能超过 120 个字符"),
  sort: z.coerce.number().int().min(0, "排序不能小于 0").default(0),
  visible: z.boolean().default(true)
});

export const permissionFormSchema = z.object({
  code: z.string().trim().min(1, "请输入权限码").max(120, "权限码不能超过 120 个字符"),
  description: optionalText(200, "描述不能超过 200 个字符"),
  kind: z.enum(["api", "button", "menu"]),
  name: z.string().trim().min(1, "请输入权限名称").max(60, "权限名称不能超过 60 个字符")
});

export type MenuForm = z.infer<typeof menuFormSchema>;
export type PermissionForm = z.infer<typeof permissionFormSchema>;
