import { SelectRatePage as SelectRatePagePrimitive } from "@scandic-hotels/booking-flow/pages/SelectRatePage"

import { getHotel } from "@/lib/trpc/memoizedRequests/getHotel"

import { getLang } from "@/i18n/serverContext"

import type { Metadata } from "next"

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

export async function generateMetadata({
  searchParams,
  params,
}: PageArgs<LangParams>): Promise<Metadata> {
  const { hotel } = await searchParams
  const { lang } = await params

  if (!hotel || Array.isArray(hotel)) {
    return {}
  }

  const hotelData = await getHotel({
    hotelId: hotel,
    language: lang,
    isCardOnlyPayment: false,
  })

  if (!hotelData?.additionalData?.name) {
    return {}
  }

  return {
    title: hotelData.additionalData.name,
  }
}

export default async function SelectRatePage(props: PageArgs<LangParams>) {
  const searchParams = await props.searchParams
  const lang = await getLang()

  return <SelectRatePagePrimitive lang={lang} searchParams={searchParams} />
}
