import * as z from "zod";
import {ProjectGroupTypeEnum} from "./types";

export const searchFormSchema = z.object({
    projectGroupCode: z.string().optional(),
    projectGroupName: z.string().optional(),
    type: z.nativeEnum(ProjectGroupTypeEnum).optional(),
    enabled: z.boolean().optional(),
});

export const formSchema = z.object({
    projectGroupCode: z.string({
        required_error: "项目组编码不能为空",
        invalid_type_error: "项目组编码格式不正确",
    })
        .min(1, "项目组编码不能为空")
        .max(50, "项目组编码不能超过50个字符")
        .regex(/^[a-z0-9-]+$/, "项目组编码只能包含小写字母、数字和连字符"),
    projectGroupName: z.string({
        required_error: "项目组名称不能为空",
        invalid_type_error: "项目组名称格式不正确",
    })
        .min(1, "项目组名称不能为空")
        .max(50, "项目组名称不能超过50个字符"),
    type: z.nativeEnum(ProjectGroupTypeEnum, {
        required_error: "请选择项目组类型",
        invalid_type_error: "项目组类型无效",
    }),
    projectGroupDesc: z.string()
        .max(200, "项目组描述不能超过200个字符")
        .nullable()
        .optional()
        .transform(val => val || ''),
    enabled: z.boolean().default(true),
    sort: z.number({
        required_error: "排序不能为空",
        invalid_type_error: "排序必须是数字",
    })
        .int("排序必须是整数")
        .min(0, "排序不能小于0")
        .default(0),
    tenantCode: z.string().default('default'),
});

export type SearchFormValues = z.infer<typeof searchFormSchema>;
export type FormValues = z.infer<typeof formSchema>; 