"use client";

import type { HotelDetailsResponse } from "@/services/hotels/hotels.client";
import {
  checkInTimeOrDefault,
  checkOutTimeOrDefault,
  type HotelMetaForRequest,
} from "@/components/hotel-review/types";
import HotelStepNav from "@/components/hotel-review/HotelStepNav";
import { HotelOverviewBusiness } from "./HotelOverviewBusiness";
import { BookingUpdateFormBusiness } from "./BookingUpdateFormBusiness";
import { RoomTypesSectionBusiness } from "./RoomTypesSectionBusiness";
import { PropertyRulesSectionBusiness } from "./PropertyRulesSectionBusiness";
import { LocationSectionBusiness } from "./LocationSectionBusiness";
import { UserReviewsSummaryBusiness } from "./UserReviewsSummaryBusiness";

interface HotelDetailsBusinessClientProps {
  hotel: HotelDetailsResponse;
}

/**
 * LEIS-987 — Business-mode hotel details, step 1 of the 3-step flow (Hotel
 * Details → Review Booking → Request approval), matching leisure-ui's real
 * hotel-details page section-for-section (overview/photos, change dates and
 * guests, room types, property rules, location, user reviews) rather than
 * the earlier, much thinner cut of this page — see each section component's
 * own doc comment for exactly what was ported as-is vs simplified and why
 * (BookingUpdateFormBusiness, LocationSectionBusiness and
 * UserReviewsSummaryBusiness all explain a real, deliberate gap from the
 * leisure-ui original). Still not ported: the search-widget-on-this-page,
 * nav tabs, recently-viewed-hotels tracking and Meta Pixel — none of those
 * are part of LEIS-987/988/989/990's scope.
 */
export function HotelDetailsBusinessClient({ hotel }: HotelDetailsBusinessClientProps) {
  const hotelMeta: HotelMetaForRequest = {
    hotelId: hotel.hotelID,
    property: hotel.hotelname,
    imageUrl: hotel.images?.[0]?.href ?? null,
    // `?? null` is not enough: a property with no rating can arrive as 0 as
    // well as null, and a 0 would fail every policy star cap while hiding the
    // property from the "Unrated hotels" flag. Only a positive rating counts.
    starRating: typeof hotel.star_rating === "number" && hotel.star_rating > 0 ? hotel.star_rating : null,
    location: [hotel.address?.city_name ?? hotel.city_name, hotel.address?.country_name]
      .filter(Boolean)
      .join(", "),
    fullAddress: [
      ...(hotel.address?.lines ?? []),
      hotel.address?.city_name ?? hotel.city_name,
      hotel.address?.country_name,
      hotel.address?.postal_code,
    ]
      .filter(Boolean)
      .join(", "),
    countryCode: hotel.address?.country_code ?? "",
    checkInTime: checkInTimeOrDefault(hotel.propertyRules?.checkInOutPolicies?.[0]?.checkIn),
    checkOutTime: checkOutTimeOrDefault(hotel.propertyRules?.checkInOutPolicies?.[0]?.checkOut),
    // Policy facts only — nothing on this page renders them (LEIS-993).
    propertyType: hotel.category ?? null,
    chainCode: hotel.chain_code ?? null,
  };

  return (
    <main className="min-h-screen bg-gray-50 pb-5">
      <div className="container webNewContent pt-4">
        {/* No onStepSelect: this page IS step 1, so there is nothing on this
         *  stepper to jump back into — see HotelStepNav's doc comment. */}
        <HotelStepNav currentStep={0} />
      </div>
      <HotelOverviewBusiness hotel={hotel} />
      <BookingUpdateFormBusiness propertyRules={hotel.propertyRules} />
      <RoomTypesSectionBusiness rooms={hotel.rooms} hotelMeta={hotelMeta} />
      <PropertyRulesSectionBusiness propertyRules={hotel.propertyRules} />
      <LocationSectionBusiness hotel={hotel} />
      <UserReviewsSummaryBusiness hotel={hotel} />
    </main>
  );
}
