import { z } from "zod"

import { imageSizesSchema } from "./image"

import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
import { CurrencyEnum } from "@/types/enums/currency"
import { PackageTypeEnum } from "@/types/enums/packages"

// TODO: Remove optional and default when the API change has been deployed
export const packagePriceSchema = z
  .object({
    currency: z.nativeEnum(CurrencyEnum).default(CurrencyEnum.Unknown),
    price: z.number(),
    totalPrice: z.number(),
  })
  .optional()
  .default({
    currency: CurrencyEnum.Unknown,
    price: 0,
    totalPrice: 0,
  })

const inventorySchema = z.object({
  date: z.string(),
  total: z.number(),
  available: z.number(),
})

export const ancillaryContentSchema = z.object({
  status: z.string(),
  id: z.string(),
  variants: z.object({
    ancillary: z.object({ id: z.string(), price: packagePriceSchema }),
    ancillaryLoyalty: z
      .object({ points: z.number(), code: z.string() })
      .optional(),
  }),
  title: z.string(),
  descriptions: z.object({ html: z.string() }),
  images: z.array(z.object({ imageSizes: imageSizesSchema })),
  requiresDeliveryTime: z.boolean(),
})

export const packageSchema = z.object({
  code: z.nativeEnum({ ...RoomPackageCodeEnum, ...BreakfastPackageEnum }),
  description: z.string(),
  inventories: z.array(inventorySchema),
  itemCode: z.string().default(""),
  localPrice: packagePriceSchema,
  requestedPrice: packagePriceSchema,
})

export const breakfastPackageSchema = z.object({
  code: z.string(),
  description: z.string(),
  localPrice: packagePriceSchema,
  requestedPrice: packagePriceSchema,
  packageType: z.enum([
    PackageTypeEnum.BreakfastAdult,
    PackageTypeEnum.BreakfastChildren,
  ]),
})

export const ancillaryPackageSchema = z.object({
  categoryName: z.string(),
  ancillaryContent: z.array(ancillaryContentSchema),
})
