import { z } from "zod"

import { imageSchema } from "@/server/routers/hotels/schemas/image"

import {
  nullableIntValidator,
  nullableNumberValidator,
} from "@/utils/zod/numberValidator"
import {
  nullableStringUrlValidator,
  nullableStringValidator,
} from "@/utils/zod/stringValidator"

import { specialAlertsSchema } from "../specialAlerts"

import { CurrencyEnum } from "@/types/enums/currency"

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

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

const contentSchema = z.object({
  images: z.array(imageSchema).default([]),
  texts: textSchema,
})

const restaurantPriceSchema = z.object({
  amount: nullableNumberValidator,
  currency: z.nativeEnum(CurrencyEnum).default(CurrencyEnum.SEK),
})

const externalBreakfastSchema = z.object({
  isAvailable: z.boolean().default(false),
  localPriceForExternalGuests: restaurantPriceSchema.optional(),
  requestedPriceForExternalGuests: restaurantPriceSchema.optional(),
})

const menuItemSchema = z.object({
  name: nullableStringValidator,
  url: nullableStringUrlValidator,
})

export const openingHoursDetailsSchema = z.object({
  alwaysOpen: z.boolean().default(false),
  closingTime: nullableStringValidator,
  isClosed: z.boolean().default(false),
  openingTime: nullableStringValidator,
  sortOrder: nullableIntValidator,
})

export const openingHoursSchema = z.object({
  friday: openingHoursDetailsSchema.optional(),
  isActive: z.boolean().default(false),
  monday: openingHoursDetailsSchema.optional(),
  name: nullableStringValidator,
  saturday: openingHoursDetailsSchema.optional(),
  sunday: openingHoursDetailsSchema.optional(),
  thursday: openingHoursDetailsSchema.optional(),
  tuesday: openingHoursDetailsSchema.optional(),
  wednesday: openingHoursDetailsSchema.optional(),
})

const openingDetailsSchema = z.object({
  alternateOpeningHours: openingHoursSchema.optional(),
  openingHours: openingHoursSchema,
  ordinary: openingHoursSchema.optional(),
  weekends: openingHoursSchema.optional(),
})

export const restaurantsSchema = z.object({
  attributes: z.object({
    bookTableUrl: nullableStringValidator,
    content: contentSchema,
    email: z.string().email().optional(),
    externalBreakfast: externalBreakfastSchema,
    isPublished: z.boolean().default(false),
    menus: z.array(menuItemSchema).default([]),
    name: z.string().default(""),
    openingDetails: z.array(openingDetailsSchema).default([]),
    phoneNumber: z.string().optional(),
    restaurantPage: z.boolean().default(false),
    elevatorPitch: z.string().optional(),
    nameInUrl: z.string().optional(),
    mainBody: z.string().optional(),
    specialAlerts: specialAlertsSchema,
  }),
  id: z.string(),
  type: z.literal("restaurants"),
})
