import z from "zod";

export const TourformSchema = z.object({
  title: z.string().min(2, {
    message: "Sarlavha kamida 2 ta belgidan iborat bo'lishi kerak",
  }),
  title_ru: z.string().min(2, {
    message: "Sarlavha kamida 2 ta belgidan iborat bo'lishi kerak",
  }),
  hotel_info: z.string().min(2, {
    message: "Sarlavha kamida 2 ta belgidan iborat bo'lishi kerak",
  }),
  hotel_info_ru: z.string(),
  hotel_meals_info: z.string().min(2, {
    message: "Sarlavha kamida 2 ta belgidan iborat bo'lishi kerak",
  }),
  hotel_meals_info_ru: z.string(),
  price: z.number().min(1000, {
    message: "Narx kamida 1000 UZS bo'lishi kerak.",
  }),
  passenger_count: z.number().min(1, {
    message: "Kamida 1 yo'lovchi bo'lishi kerak.",
  }),
  min_person: z.number().min(1, {
    message: "Kamida 1 yo'lovchi bo'lishi kerak.",
  }),
  max_person: z.number().min(1, {
    message: "Kamida 1 yo'lovchi bo'lishi kerak.",
  }),
  departure: z.string().min(2, {
    message: "Ketish joyi eng kamida 2 ta belgidan iborat bo'lishi kerak.",
  }),
  departure_ru: z.string().min(2, {
    message: "Ketish joyi eng kamida 2 ta belgidan iborat bo'lishi kerak.",
  }),
  destination: z.string().min(2, {
    message: "Borish joyi eng kamida 2 ta belgidan iborat bo'lishi kerak.",
  }),
  destination_ru: z.string().min(2, {
    message: "Borish joyi eng kamida 2 ta belgidan iborat bo'lishi kerak.",
  }),
  location_name: z.string().min(2, {
    message: "Eng kamida 2 ta belgidan iborat bo'lishi kerak.",
  }),
  location_name_ru: z.string().min(2, {
    message: "Eng kamida 2 ta belgidan iborat bo'lishi kerak.",
  }),
  visa_required: z
    .enum(["no", "yes"], { message: "Iltimos, visa talabliligini tanlang" })
    .refine((val) => val === "no" || val === "yes", {
      message: "Iltimos, visa talabliligini tanlang",
    }),
  departureDateTime: z.object({
    date: z.date({ message: "Jo'nash vaqti majburiy" }),
    time: z
      .string()
      .min(1, { message: "Jo'nash vaqti majburiy" })
      .refine(
        (val) => {
          const parts = val.split(":");
          if (parts.length !== 3) return false;
          const [hour, minute, second] = parts.map(Number);
          return (
            !isNaN(hour) &&
            !isNaN(minute) &&
            !isNaN(second) &&
            hour >= 0 &&
            hour <= 23 &&
            minute >= 0 &&
            minute <= 59 &&
            second >= 0 &&
            second <= 59
          );
        },
        { message: "Yaroqli vaqt kiriting (masalan, 08:30:00)" },
      ),
  }),
  travelDateTime: z.object({
    date: z.date({ message: "Jo'nash vaqti majburiy" }),
    time: z
      .string()
      .min(1, { message: "Jo'nash vaqti majburiy" })
      .refine(
        (val) => {
          const parts = val.split(":");
          if (parts.length !== 3) return false;
          const [hour, minute, second] = parts.map(Number);
          return (
            !isNaN(hour) &&
            !isNaN(minute) &&
            !isNaN(second) &&
            hour >= 0 &&
            hour <= 23 &&
            minute >= 0 &&
            minute <= 59 &&
            second >= 0 &&
            second <= 59
          );
        },
        { message: "Yaroqli vaqt kiriting (masalan, 08:30:00)" },
      ),
  }),
  languages: z.string().min(1, { message: "Majburiy maydon" }),
  duration: z.number().min(1, { message: "Kamida 1 kun bo'lishi kerak" }),
  badges: z.array(z.number()).optional(),
  tarif: z
    .array(
      z.object({
        tariff: z.number().min(1, { message: "Transport ID majburiy" }),
        price: z
          .number()
          .min(0, { message: "Narx 0 dan kichik bo'lishi mumkin emas" }),
      }),
    )
    .optional(),
  transport: z
    .array(
      z.object({
        transport: z.number().min(1, { message: "Transport ID majburiy" }),
        price: z
          .number()
          .min(0, { message: "Narx 0 dan kichik bo'lishi mumkin emas" }),
      }),
    )
    .optional(),
  banner: z.any().nullable(),
  images: z
    .array(
      z.object({
        id: z.number().optional(),
        image: z.union([z.instanceof(File), z.string()]).nullable(),
      }),
    )
    .min(1, { message: "Kamida bitta rasm kiriting." }),
  amenities: z.array(z.number()).optional(),

  hotel_services: z
    .array(
      z.object({
        id: z.number().optional(),
        image: z.any().nullable(),
        title: z.string().min(1, "Xizmat nomi majburiy"),
        title_ru: z.string().min(1, { message: "Majburiy maydon" }),
        description: z.string().min(1, "Tavsif majburiy"),
        desc_ru: z.string().min(1, { message: "Majburiy maydon" }),
      }),
    )
    .optional(),

  hotel_meals: z
    .array(
      z.object({
        id: z.number().optional(),
        image: z.any().nullable(),
        title: z.string().min(1, "Xizmat nomi majburiy"),
        title_ru: z.string().min(1, "Majburiy maydon"),
        description: z.string().min(1, "Tavsif majburiy"),
        desc_ru: z.string().min(1, "Majburiy maydon"),
      }),
    )
    .min(1, { message: "Kamida bitta xizmat kiriting." }),

  ticket_itinerary: z
    .array(
      z.object({
        id: z.number().optional(), // Edit uchun
        ticket_itinerary_image: z.array(
          z.union([
            z.object({
              image: z.union([z.instanceof(File), z.string()]),
            }),
            z.instanceof(File),
            z.string(),
          ]),
        ),
        title: z.string().min(1, "Sarlavha majburiy"),
        title_ru: z.string().min(1, "Sarlavha (RU) majburiy"),
        duration: z.number().min(1, { message: "Kamida 1 kun bo'lishi kerak" }),
        ticket_itinerary_destinations: z.array(
          z.object({
            name: z.string().min(1, { message: "Majburiy maydon" }),
            name_ru: z.string().min(1, { message: "Majburiy maydon" }),
          }),
        ),
      }),
    )
    .min(1, { message: "Kamida bitta xizmat kiriting." }),

  extra_service: z
    .array(
      z.object({
        name: z.string().min(1, { message: "Xizmat nomi majburiy" }),
        name_ru: z.string().min(1, { message: "Xizmat nomi (RU) majburiy" }),
      }),
    )
    .optional(),

  paid_extra_service: z
    .array(
      z.object({
        name: z.string().min(1, { message: "Xizmat nomi majburiy" }),
        name_ru: z.string().min(1, { message: "Xizmat nomi (RU) majburiy" }),
        price: z
          .number()
          .min(0, { message: "Narx manfiy bo'lishi mumkin emas." }),
      }),
    )
    .optional(),
});
