import { z } from "zod"

import {
  nullableArrayObjectValidator,
  nullableArrayStringValidator,
} from "@/utils/zod/arrayValidator"
import { nullableNumberValidator } from "@/utils/zod/numberValidator"
import { nullableStringValidator } from "@/utils/zod/stringValidator"

import { addressSchema } from "./hotel/address"
import { contactInformationSchema } from "./hotel/contactInformation"
import { hotelContentSchema } from "./hotel/content"
import { detailedFacilitiesSchema } from "./hotel/detailedFacility"
import { hotelFactsSchema } from "./hotel/facts"
import { healthFacilitiesSchema } from "./hotel/healthFacilities"
import { includeSchema } from "./hotel/include/include"
import { locationSchema } from "./hotel/location"
import { merchantInformationSchema } from "./hotel/merchantInformation"
import { parkingSchema } from "./hotel/parking"
import { pointOfInterestsSchema } from "./hotel/poi"
import { ratingsSchema } from "./hotel/rating"
import { rewardNightSchema } from "./hotel/rewardNight"
import { socialMediaSchema } from "./hotel/socialMedia"
import { specialAlertsSchema } from "./hotel/specialAlerts"
import { imageSchema } from "./image"

export const attributesSchema = z.object({
  address: addressSchema,
  cityId: nullableStringValidator,
  cityName: nullableStringValidator,
  contactInformation: contactInformationSchema,
  countryCode: nullableStringValidator,
  detailedFacilities: detailedFacilitiesSchema,
  galleryImages: z
    .array(imageSchema)
    .nullish()
    .transform((arr) => (arr ? arr.filter(Boolean) : [])),
  healthFacilities: healthFacilitiesSchema,
  hotelContent: hotelContentSchema,
  hotelFacts: hotelFactsSchema,
  hotelType: nullableStringValidator,
  isActive: z.boolean(),
  isPublished: z.boolean(),
  keywords: nullableArrayStringValidator,
  location: locationSchema,
  merchantInformationData: merchantInformationSchema,
  name: nullableStringValidator,
  operaId: nullableStringValidator,
  parking: nullableArrayObjectValidator(parkingSchema),
  pointsOfInterest: pointOfInterestsSchema,
  ratings: ratingsSchema,
  rewardNight: rewardNightSchema,
  socialMedia: socialMediaSchema,
  specialAlerts: specialAlertsSchema,
  vat: nullableNumberValidator,
})

export const includedSchema = z
  .array(includeSchema)
  .default([])
  .transform((data) =>
    data.filter((item) => {
      if (item) {
        if ("isPublished" in item && item.isPublished === false) {
          return false
        }
        return true
      }
      return false
    })
  )

const relationshipSchema = z.object({
  links: z.object({
    related: z.string(),
  }),
})

export const relationshipsSchema = z.object({
  meetingRooms: relationshipSchema,
  nearbyHotels: relationshipSchema,
  restaurants: relationshipSchema,
  roomCategories: relationshipSchema,
})
