import { z } from "zod"

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

import { imageSchema } from "../image"

const healthFacilitiesOpenHoursSchema = z.object({
  alwaysOpen: z.boolean(),
  closingTime: nullableStringValidator,
  isClosed: z.boolean(),
  openingTime: nullableStringValidator,
  sortOrder: nullableNumberValidator,
})

const descriptionSchema = z
  .object({
    medium: nullableStringValidator,
    short: nullableStringValidator,
  })
  .nullish()

const detailsSchema = z.object({
  name: nullableStringValidator,
  type: nullableStringValidator,
  value: nullableStringValidator,
})

const textsSchema = z.object({
  descriptions: descriptionSchema,
  facilityInformation: nullableStringValidator,
  meetingDescription: descriptionSchema,
  surroundingInformation: nullableStringValidator,
})

export const healthFacilitySchema = z.object({
  content: z.object({
    images: z
      .array(imageSchema)
      .nullish()
      .transform((arr) => (arr ? arr.filter(Boolean) : [])),
    texts: textsSchema,
  }),
  details: nullableArrayObjectValidator(detailsSchema),
  openingDetails: z.object({
    manualOpeningHours: nullableStringValidator,
    openingHours: z.object({
      ordinary: healthFacilitiesOpenHoursSchema,
      weekends: healthFacilitiesOpenHoursSchema,
    }),
    useManualOpeningHours: z
      .boolean()
      .nullish()
      .transform((b) => !!b),
  }),
  type: nullableStringValidator,
})

export const healthFacilitiesSchema =
  nullableArrayObjectValidator(healthFacilitySchema)
