import { env } from "@/env/server"
import { getHotel, getHotelPage } from "@/lib/trpc/memoizedRequests"

import { BookingWidget } from "@/components/BookingWidget"
import { getLang } from "@/i18n/serverContext"
import { parseBookingWidgetSearchParams } from "@/utils/url"

import type { NextSearchParams, PageArgs } from "@/types/params"

export default async function BookingWidgetHotelPage(
  props: PageArgs<{}, NextSearchParams & { subpage?: string }>
) {
  const searchParams = await props.searchParams
  if (env.NEW_SITE_LIVE_STATUS === "NOT_LIVE") {
    return null
  }

  const hotelPageData = await getHotelPage()
  const hotelData = await getHotel({
    hotelId: hotelPageData?.hotel_page_id || "",
    language: await getLang(),
    isCardOnlyPayment: false,
  })

  const subpage = searchParams.subpage
  const isMeetingSubpage =
    subpage && hotelData?.additionalData.meetingRooms.nameInUrl === subpage

  if (isMeetingSubpage) {
    return null
  }

  const bookingWidgetSearchParams = {
    bookingCode: searchParams.bookingCode ?? "",
    hotel: hotelData?.hotel.id ?? "",
    city: hotelData?.hotel.cityName ?? "",
  }

  const booking = parseBookingWidgetSearchParams(bookingWidgetSearchParams)

  return <BookingWidget booking={booking} />
}
