import { z } from "zod"

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

const awardSchema = z.object({
  displayName: nullableStringValidator,
  images: z
    .object({
      large: nullableStringValidator,
      medium: nullableStringValidator,
      small: nullableStringValidator,
    })
    .nullish()
    .transform((obj) =>
      obj
        ? obj
        : {
            small: "",
            medium: "",
            large: "",
          }
    ),
})

const reviewsSchema = z
  .object({
    widgetHtmlTagId: nullableStringValidator,
    widgetScriptEmbedUrlIframe: nullableStringValidator,
    widgetScriptEmbedUrlJavaScript: nullableStringValidator,
  })
  .nullish()
  .transform((obj) =>
    obj
      ? obj
      : {
          widgetHtmlTagId: "",
          widgetScriptEmbedUrlIframe: "",
          widgetScriptEmbedUrlJavaScript: "",
        }
  )

export const ratingsSchema = z
  .object({
    tripAdvisor: z.object({
      awards: nullableArrayObjectValidator(awardSchema),
      numberOfReviews: z.number(),
      rating: z.number(),
      ratingImageUrl: nullableStringUrlValidator,
      reviews: reviewsSchema,
      webUrl: nullableStringUrlValidator,
    }),
  })
  .optional()
