import { z } from "zod";

export const MAX_FILE_SIZE = 2 * 1024 * 1024; // 2 МБ

const msg = (ru: string, en: string, tm: string, lang: string) =>
  lang === "ru" ? ru : lang === "tm" ? tm : en;

export const createFormSchema = (lang: string) =>
  z.object({
    type: z.string(),
    company_name: z
      .string()
      .min(3, {
        message: msg(
          "Название компании должно быть не менее 3 символов",
          "Company name must be at least 3 characters",
          "Kompaniýanyň ady azyndan 3 simwoldan ybarat bolmaly",
          lang
        ),
      }),
    representative_name: z
      .string()
      .min(3, {
        message: msg(
          "Имя представителя должно быть не менее 3 символов",
          "Representative name must be at least 3 characters",
          "Wekiliniň ady azyndan 3 simwoldan ybarat bolmaly",
          lang
        ),
      }),
    job_title: z
      .string()
      .min(3, {
        message: msg(
          "Должность должна быть не менее 3 символов",
          "Job title must be at least 3 characters",
          "Wezipesi azyndan 3 simwoldan ybarat bolmaly",
          lang
        ),
      }),
    participants_number: z
      .string()
      .min(1, {
        message: msg(
          "Укажите количество участников",
          "Specify the number of participants",
          "Gatnaşyjylaryň sanyny görkeziň",
          lang
        ),
      }),
    country: z
      .string()
      .min(3, {
        message: msg(
          "Название страны должно быть не менее 3 символов",
          "Country must be at least 3 characters",
          "Ýurduň ady azyndan 3 simwoldan ybarat bolmaly",
          lang
        ),
      }),
    email_address: z.string().email({
      message: msg(
        "Укажите корректный email",
        "Enter a valid email",
        "Dogry email salgysyny giriziň",
        lang
      ),
    }),
    phone_number: z
      .string()
      .min(8, {
        message: msg(
          "Номер телефона должен быть не менее 8 символов",
          "Phone number must be at least 8 characters",
          "Telefon belgisi azyndan 8 simwoldan ybarat bolmaly",
          lang
        ),
      }),
    website: z.string().optional(),

    meeting_objective: z.string().min(3, {
      message: msg(
        "Укажите цель встречи",
        "Specify the meeting objective",
        "Duşuşygyň maksadyny görkeziň",
        lang
      ),
    }),
    proposal_description: z.string().optional(),
    government_agency: z.string().optional(),
    sector_industry: z.string().optional(),
    key_services: z.string().optional(),
    government_experience: z.string().optional(),

    preferred_meeting_datetime: z.string().optional(),
    meeting_mode: z.string().optional(),
    language_preference: z.string().optional(),
    technical_requirements: z.string().optional(),
    company_profile: z
      .custom<File>((value) => value instanceof File, {
        message: msg(
          "Выберите корректный файл",
          "Select a valid file",
          "Dogry faýly saýlaň",
          lang
        ),
      })
      .refine(
        (file) =>
          file &&
          ["image/jpeg", "image/png", "application/pdf"].includes(file.type),
        {
          message: msg(
            "Допускаются только файлы JPG, PNG и PDF",
            "Only JPG, PNG and PDF files are allowed",
            "Diňe JPG, PNG we PDF faýllary kabul edilýär",
            lang
          ),
        }
      )
      .optional(),
    proposal_presentation: z
      .custom<File>((value) => value instanceof File, {
        message: msg(
          "Выберите корректный файл",
          "Select a valid file",
          "Dogry faýly saýlaň",
          lang
        ),
      })
      .refine(
        (file) =>
          file &&
          ["image/jpeg", "image/png", "application/pdf"].includes(file.type),
        {
          message: msg(
            "Допускаются только файлы JPG, PNG и PDF",
            "Only JPG, PNG and PDF files are allowed",
            "Diňe JPG, PNG we PDF faýllary kabul edilýär",
            lang
          ),
        }
      )
      .optional(),
    relevant_certification: z
      .custom<File>((value) => value instanceof File, {
        message: msg(
          "Выберите корректный файл",
          "Select a valid file",
          "Dogry faýly saýlaň",
          lang
        ),
      })
      .refine(
        (file) =>
          file &&
          ["image/jpeg", "image/png", "application/pdf"].includes(file.type),
        {
          message: msg(
            "Допускаются только файлы JPG, PNG и PDF",
            "Only JPG, PNG and PDF files are allowed",
            "Diňe JPG, PNG we PDF faýllary kabul edilýär",
            lang
          ),
        }
      )
      .optional(),
  });

export type FormType = z.infer<ReturnType<typeof createFormSchema>>;

export const defaultValuesOfB2b = {
  type: "B2B",
  company_name: "",
  representative_name: "",
  job_title: "",
  participants_number: "",
  country: "",
  email_address: "",
  phone_number: "",
  website: "",
  meeting_objective: "",
  proposal_description: "",
  government_agency: "",
  sector_industry: "",
  key_services: "",
  government_experience: "",
  preferred_meeting_datetime: "",
  meeting_mode: "",
  language_preference: "",
  technical_requirements: "",
};
