import { cache } from "react"

import { Lang } from "@/constants/languages"
import {
  GetRoomsAvailabilityInput,
  GetSelectedRoomAvailabilityInput,
} from "@/server/routers/hotels/input"

import { serverClient } from "../server"

import type {
  BreackfastPackagesInput,
  PackagesInput,
} from "@/types/requests/packages"

export const getLocations = cache(async function getMemoizedLocations() {
  return serverClient().hotel.locations.get()
})

export const getProfile = cache(async function getMemoizedProfile() {
  return serverClient().user.get()
})

export const getName = cache(async function getMemoizedName() {
  return serverClient().user.name()
})

export const getProfileSafely = cache(
  async function getMemoizedProfileSafely() {
    return serverClient().user.getSafely()
  }
)

export const getCreditCardsSafely = cache(
  async function getMemoizedCreditCardsSafely() {
    return serverClient().user.safeCreditCards()
  }
)

export const getMembershipLevel = cache(
  async function getMemoizedMembershipLevel() {
    return serverClient().user.membershipLevel()
  }
)

export const getMembershipLevelSafely = cache(
  async function getMemoizedMembershipLevelSafely() {
    return serverClient().user.safeMembershipLevel()
  }
)

export const getMembershipCards = cache(
  async function getMemoizedMembershipCards() {
    return serverClient().user.membershipCards()
  }
)

export const getUserTracking = cache(async function getMemoizedUserTracking() {
  return serverClient().user.tracking()
})

export const getHotelData = cache(async function getMemoizedHotelData({
  hotelId,
  language,
  isCardOnlyPayment,
}: {
  hotelId: string
  language: string
  isCardOnlyPayment?: boolean
}) {
  return serverClient().hotel.hotelData.get({
    hotelId,
    language,
    isCardOnlyPayment,
  })
})

export const getRoomAvailability = cache(
  async function getMemoizedRoomAvailability({
    hotelId,
    adults,
    roomStayStartDate,
    roomStayEndDate,
    children,
    promotionCode,
    rateCode,
  }: GetRoomsAvailabilityInput) {
    return serverClient().hotel.availability.rooms({
      hotelId,
      adults,
      roomStayStartDate,
      roomStayEndDate,
      children,
      promotionCode,
      rateCode,
    })
  }
)

export const getSelectedRoomAvailability = cache(
  async function getMemoizedRoomAvailability(
    args: GetSelectedRoomAvailabilityInput
  ) {
    return serverClient().hotel.availability.room(args)
  }
)

export const getFooter = cache(async function getMemoizedFooter() {
  return serverClient().contentstack.base.footer()
})

export const getHeader = cache(async function getMemoizedHeader() {
  return serverClient().contentstack.base.header()
})

export const getCurrentHeader = cache(async function getMemoizedCurrentHeader(
  lang: Lang
) {
  return serverClient().contentstack.base.currentHeader({ lang })
})

export const getCurrentFooter = cache(async function getMemoizedCurrentFooter(
  lang: Lang
) {
  return serverClient().contentstack.base.currentFooter({ lang })
})

export const getMyPagesNavigation = cache(
  async function getMemoizedMyPagesNavigation() {
    return serverClient().contentstack.myPages.navigation.get()
  }
)

export const getLanguageSwitcher = cache(
  async function getMemoizedLanguageSwitcher() {
    return serverClient().contentstack.languageSwitcher.get()
  }
)

export const getSiteConfig = cache(async function getMemoizedSiteConfig() {
  return serverClient().contentstack.base.siteConfig()
})

export const getBreakfastPackages = cache(async function getMemoizedPackages(
  input: BreackfastPackagesInput
) {
  return serverClient().hotel.packages.breakfast(input)
})

export const getPackages = cache(async function getMemoizedPackages(
  input: PackagesInput
) {
  return serverClient().hotel.packages.get(input)
})

export const getBookingConfirmation = cache(
  function getMemoizedBookingConfirmation(confirmationNumber: string) {
    return serverClient().booking.confirmation({ confirmationNumber })
  }
)
