import { z } from "zod"

import { nullableArrayObjectValidator } from "@/utils/zod/arrayValidator"
import { nullableStringValidator } from "@/utils/zod/stringValidator"

import { displayWebPageSchema } from "./additionalData/displayWebPage"
import { facilitySchema } from "./additionalData/facility"
import { gallerySchema } from "./additionalData/gallery"
import { restaurantsOverviewPageSchema } from "./additionalData/restaurantsOverviewPage"
import { specialNeedGroupSchema } from "./additionalData/specialNeedGroups"

export const extraPageSchema = z.object({
  elevatorPitch: nullableStringValidator,
  mainBody: nullableStringValidator,
  nameInUrl: nullableStringValidator,
})

export const additionalDataAttributesSchema = z.object({
  accessibility: facilitySchema.nullish(),
  conferencesAndMeetings: facilitySchema.nullish(),
  displayWebPage: displayWebPageSchema,
  gallery: gallerySchema.nullish(),
  healthAndFitness: extraPageSchema,
  healthAndWellness: facilitySchema.nullish(),
  hotelParking: extraPageSchema,
  hotelRoomElevatorPitchText: nullableStringValidator,
  hotelSpecialNeeds: extraPageSchema,
  id: nullableStringValidator,
  meetingRooms: extraPageSchema.merge(
    z.object({
      meetingOnlineLink: z.string().nullish(),
    })
  ),
  name: nullableStringValidator,
  parkingImages: facilitySchema.nullish(),
  restaurantImages: facilitySchema.nullish(),
  restaurantsOverviewPage: restaurantsOverviewPageSchema,
  specialNeedGroups: nullableArrayObjectValidator(specialNeedGroupSchema),
})

export const additionalDataSchema = z.object({
  attributes: additionalDataAttributesSchema,
  type: z.literal("additionalData"),
})

export function transformAdditionalData(
  data: z.output<typeof additionalDataSchema>
) {
  return {
    ...data.attributes,
    id: data.attributes.id,
    type: data.type,
  }
}
