import { z } from 'zod';

export type ProcessStepFormValues = {
    step_number: number;
    icon: string;
    title: { id: string; en: string };
    slug: { id: string; en: string };
    description: { id: string; en: string };
    is_active: boolean;
    sort_order: number;
};

const translatableField = (max: number) =>
    z.object({
        id: z.string().max(max, `Maksimal ${max} karakter`),
        en: z.string().max(max, `Maksimal ${max} karakter`),
    });

export const processStepSchema = z.object({
    step_number: z.coerce.number().int().min(1, 'Minimal 1').max(20, 'Maksimal 20'),
    icon: z.string().max(50).optional().or(z.literal('')),
    title: translatableField(255).superRefine((value, ctx) => {
        if (!value.id.trim()) {
            ctx.addIssue({ code: 'custom', message: 'Judul (ID) wajib diisi', path: ['id'] });
        }

        if (!value.en.trim()) {
            ctx.addIssue({ code: 'custom', message: 'Title (EN) is required', path: ['en'] });
        }
    }),
    description: translatableField(1000),
    slug: z.object({
        id: z.string().max(255).regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$|^$/, 'Format slug tidak valid'),
        en: z.string().max(255).regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$|^$/, 'Invalid slug format'),
    }),
    is_active: z.boolean(),
    sort_order: z.coerce.number().int().min(0, 'Minimal 0'),
}) as unknown as z.ZodType<ProcessStepFormValues, ProcessStepFormValues>;
