import z from "zod";

const fileSchema = z.union([
  z.instanceof(File, { message: "Rasm faylini yuklang" }),
  z.string().min(1, { message: "Rasm faylini yuklang" }),
]);

export const newsForm = z.object({
  title: z.string().min(2, {
    message: "Kamida 2 ta belgidan iborat bo‘lishi kerak.",
  }),
  title_ru: z.string().min(2, {
    message: "Kamida 2 ta belgidan iborat bo‘lishi kerak.",
  }),
  desc: z.string().min(2, {
    message: "Kamida 2 ta belgidan iborat bo‘lishi kerak.",
  }),
  desc_ru: z.string().min(2, {
    message: "Kamida 2 ta belgidan iborat bo‘lishi kerak.",
  }),
  category: z.number().min(1, {
    message: "Majburiy maydon",
  }),
  banner: fileSchema,
});

// zod schema ni yangilaymiz
export const newsPostForm = z.object({
  desc: z
    .string()
    .min(2, { message: "Kamida 2 ta belgidan iborat bo'lishi kerak." }),
  desc_ru: z
    .string()
    .min(2, { message: "Kamida 2 ta belgidan iborat bo'lishi kerak." }),
  is_public: z.enum(["no", "yes"], { message: "Iltimos, tanlang" }),

  sections: z
    .array(
      z.object({
        image: z.union([z.instanceof(File), z.string()]).optional(),
        text: z.string().optional(),
        text_ru: z.string().optional(),
      }),
    )
    .optional(),

  post_tags: z
    .array(
      z.object({
        name: z.string().min(1, { message: "Teg bo'sh bo'lmasligi kerak." }),
        name_ru: z
          .string()
          .min(1, { message: "Teg (RU) bo'sh bo'lmasligi kerak." }),
      }),
    )
    .min(1, { message: "Kamida bitta teg kiriting." }),
});

export type NewsPostFormType = z.infer<typeof newsPostForm>;
