//----------------------------------------------------------------------------------------

import { z } from "zod";
import {
  ACCEPTED_DOCS_TYPES,
  ACCEPTED_IMAGE_TYPES,
  ACCEPTED_MEDIA_TYPES,
  MAX_FILE_SIZE,
} from "../../utils/consts";

//----------------------------------------------------------------------------------------

export const imageSchema = z
  .any()
  .refine((file: any | null) => !!file, {
    message: "L'image est obligatoire",
  })
  .refine(
    (file: any) => {
      return file?.file.bytesRead <= MAX_FILE_SIZE;
    },
    {
      message: "La taille de l'image est supérieure à 50 Mo.",
    }
  )
  .refine((file: any) => ACCEPTED_IMAGE_TYPES.includes(file.mimetype), {
    message: "Seuls les formats .jpg, .jpeg, .png et .webp sont acceptés",
  });

export const documentSchema = z
  .any()
  .refine((file: any | null) => !!file, {
    message: "Le document est obligatoire",
  })
  .refine(
    (file: any) => {
      return file?.file.bytesRead <= MAX_FILE_SIZE;
    },
    {
      message: "La taille du document est supérieure à 50 Mo.",
    }
  )
  .refine((file: any) => ACCEPTED_DOCS_TYPES.includes(file.mimetype), {
    message: "Le format est inacceptable",
  });

export const mediaSchema = z
  .any()
  .refine((file: any | null) => !!file, {
    message: "Le document est obligatoire",
  })
  .refine(
    (file: any) => {
      return file?.file.bytesRead <= MAX_FILE_SIZE;
    },
    {
      message: "La taille du document est supérieure à 50 Mo.",
    }
  )
  .refine((file: any) => ACCEPTED_MEDIA_TYPES.includes(file.mimetype), {
    message: "Le format est inacceptable",
  });

export const pdfSchema = z
  .any()
  .refine((file: any | null) => !!file, {
    message: "Le document est obligatoire",
  })
  .refine(
    (file: any) => {
      return file?.file.bytesRead <= MAX_FILE_SIZE;
    },
    {
      message: "La taille du document est supérieure à 50 Mo.",
    }
  )
  .refine((file: any) => file.mimetype === "application/pdf", {
    message: "Le format est inacceptable",
  });

//--------------------------------------------------------------------------------------

export const minioImageSchema = z.object({
  image: imageSchema,
});

export type MinioImageSchema = z.infer<typeof minioImageSchema>;

//--------------------------------------------------------------------------------------

export const minioDocumentSchema = z.object({
  document: documentSchema,
});

export type MinioDocumentSchema = z.infer<typeof minioDocumentSchema>;

//--------------------------------------------------------------------------------------

export const minioMediaSchema = z.object({
  document: mediaSchema,
});

export type MinioMediatSchema = z.infer<typeof minioMediaSchema>;

//--------------------------------------------------------------------------------------

export const minioPdfSchema = z.object({
  document: pdfSchema,
});

export type MinioPdfSchema = z.infer<typeof minioPdfSchema>;

//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------

export const minioDeleteFileSchema = z.object({
  path: z.string({ required_error: "Le fichier est obligatoirs" }),
});

export type MinioDeleteFileSchema = z.infer<typeof minioDeleteFileSchema>;

//--------------------------------------------------------------------------------------
