import { notFound } from "next/navigation"

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

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

import AccessibilitySubpage from "./AccessibilitySubpage"
import MeetingsSubpage from "./MeetingsSubpage"
import ParkingSubpage from "./ParkingSubpage"
import RestaurantSubpage from "./RestaurantSubpage"
import ReviewsSubpage from "./ReviewsSubpage"
import { verifySubpageShouldExist } from "./utils"
import WellnessSubpage from "./WellnessSubpage"

import type { HotelSubpageProps } from "@/types/components/hotelPage/subpage"

export default async function HotelSubpage({
  hotelId,
  subpage,
}: HotelSubpageProps) {
  const lang = await getLang()
  const [hotelPageData, hotelData] = await Promise.all([
    getHotelPage(),
    getHotel({ hotelId, language: lang, isCardOnlyPayment: false }),
  ])

  if (!hotelData?.hotel || !hotelPageData) {
    notFound()
  }

  if (!verifySubpageShouldExist(hotelData, subpage)) {
    notFound()
  }

  const { hotel, additionalData, restaurants } = hotelData

  const currentRestaurant = restaurants.find(
    (restaurant) => restaurant.nameInUrl === subpage
  )
  if (currentRestaurant) {
    return (
      <RestaurantSubpage
        restaurant={currentRestaurant}
        hotelAddress={hotel.address}
      />
    )
  }

  switch (subpage) {
    case additionalData.meetingRooms.nameInUrl:
      return (
        <MeetingsSubpage
          hotelId={hotelId}
          hotel={hotel}
          additionalData={additionalData}
        />
      )
    case additionalData.healthAndFitness.nameInUrl:
      return (
        <WellnessSubpage
          hotel={hotel}
          healthAndFitness={additionalData.healthAndFitness}
        />
      )
    case additionalData.hotelParking.nameInUrl:
      return <ParkingSubpage hotel={hotel} additionalData={additionalData} />
    case additionalData.hotelSpecialNeeds.nameInUrl:
      return (
        <AccessibilitySubpage
          hotelName={hotel.name}
          additionalData={additionalData}
        />
      )
    case "reviews":
      return <ReviewsSubpage hotel={hotel} />
    default:
      notFound()
  }
}
