import { redirect } from "next/navigation";

import { getLiveHotelDetails } from "@/services/hotels/hotels.client";
import { HotelDetailsBusinessClient } from "@/components/hotel-details/business/HotelDetailsBusinessClient";

export const dynamic = "force-dynamic";
export const revalidate = 0;
export const fetchCache = "force-no-store";

export const metadata = {
  title: "Hotel details | Leisure Business",
  description: "Review the hotel and room rate, then send it for approval",
};

interface HotelDetailsPageProps {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}

/**
 * LEIS-987 — Business-mode hotel details.
 *
 * Thin shell, same shape as app/flight-request/page.tsx: parse the params
 * HotelCard.tsx already builds (hotelId, checkinDate, checkoutDate, rooms,
 * adults, children), fetch live, hand off to the client tree. Unlike the
 * flight review page this fetches server-side rather than from a client
 * loader — hotel-details has no equivalent of Price #1's "must paint before
 * a slow priced-offer call resolves" problem, since the search-service call
 * here is the same one `/srp/hotels` already makes synchronously.
 *
 * `getLiveHotelDetails` never falls back to sample data (see that function's
 * own doc comment) — a missing/failed fetch here means there is nothing a
 * Business request could safely be built from, so it reads as "not found"
 * rather than rendering leisure-ui's marketing placeholder hotel.
 */
export default async function HotelDetailsPage({ searchParams }: HotelDetailsPageProps) {
  const params = await searchParams;

  const first = (value: string | string[] | undefined): string | undefined =>
    Array.isArray(value) ? value[0] : value;

  const hotelId = first(params.hotelId)?.trim() || "";
  const checkinDate = first(params.checkinDate)?.trim() || "";
  const checkoutDate = first(params.checkoutDate)?.trim() || "";
  const rooms = first(params.rooms)?.trim() || "1";
  const adults = first(params.adults)?.trim() || "1";
  const children = first(params.children)?.trim();

  // No hotel to show. Nothing on this page can be rendered or recovered from
  // a missing hotelId, so send them back to search rather than show an error
  // screen about a URL they did not type — same rule flight-request follows
  // for a missing clientRef.
  if (!hotelId || !checkinDate || !checkoutDate) {
    redirect("/srp/hotels");
  }

  const hotel = await getLiveHotelDetails({
    hotelId,
    checkinDate,
    checkoutDate,
    rooms,
    adults,
    children,
  });

  if (!hotel) {
    return (
      <main className="min-h-screen bg-gray-50 pb-5">
        <div
          className="d-flex justify-content-center align-items-center flex-column gap-3"
          style={{ height: "40vh" }}
        >
          <h2>This hotel is not available for those dates</h2>
          <a href="/srp/hotels" className="color-text-first fw-bold">
            Back to search
          </a>
        </div>
      </main>
    );
  }

  return <HotelDetailsBusinessClient hotel={hotel} />;
}
