import { z } from "zod";

const userStatusSchema = z.enum(["active", "disabled", "locked"]);
const roleIdsSchema = z.array(z.coerce.number().int().positive()).default([]);

const userBaseSchema = z.object({
  mfaEnabled: z.boolean().default(false),
  name: z.string().trim().min(1, "请输入姓名").max(50, "姓名不能超过 50 个字符"),
  roleIds: roleIdsSchema,
  status: userStatusSchema,
  team: z.string().trim().max(80, "团队不能超过 80 个字符").optional().default(""),
  teamId: z.union([z.literal(""), z.coerce.number().int().positive("请选择团队")]).optional().transform((value) => value === "" ? undefined : value)
});

export const userCreateFormSchema = userBaseSchema.extend({
  password: z.string().trim().max(72, "密码不能超过 72 个字符").optional().default(""),
  username: z.string().trim().min(1, "请输入账号").max(50, "账号不能超过 50 个字符")
});

export const userUpdateFormSchema = userBaseSchema;

export type UserCreateForm = z.infer<typeof userCreateFormSchema>;
export type UserUpdateForm = z.infer<typeof userUpdateFormSchema>;
