import { z } from "zod"

import { discriminatedUnionArray } from "@/lib/discriminatedUnion"

import {
  activitiesCardRefSchema,
  activitiesCardSchema,
} from "../schemas/blocks/activitiesCard"
import { hotelFaqRefsSchema, hotelFaqSchema } from "../schemas/blocks/hotelFaq"
import { systemSchema } from "../schemas/system"

import { HotelPageEnum } from "@/types/enums/hotelPage"

const contentBlockActivities = z
  .object({
    __typename: z.literal(HotelPageEnum.ContentStack.blocks.ActivitiesCard),
  })
  .merge(activitiesCardSchema)

export const contentBlock = z.discriminatedUnion("__typename", [
  contentBlockActivities,
])

export const hotelPageSchema = z.object({
  hotel_page: z.object({
    content: discriminatedUnionArray(contentBlock.options).nullable(),
    faq: hotelFaqSchema,
    hotel_page_id: z.string(),
    title: z.string(),
    url: z.string(),
    system: systemSchema.merge(
      z.object({
        created_at: z.string(),
        updated_at: z.string(),
      })
    ),
  }),
})

/** REFS */
const hotelPageActiviesCardRefs = z
  .object({
    __typename: z.literal(HotelPageEnum.ContentStack.blocks.ActivitiesCard),
  })
  .merge(activitiesCardRefSchema)

const hotelPageBlockRefsItem = z.discriminatedUnion("__typename", [
  hotelPageActiviesCardRefs,
])

export const hotelPageRefsSchema = z.object({
  hotel_page: z.object({
    content: discriminatedUnionArray(hotelPageBlockRefsItem.options).nullable(),
    faq: hotelFaqRefsSchema.nullable(),
    system: systemSchema,
  }),
})
