import type { ResponseHotelData } from "@/types/hotel";
import { formatHotelName, mapAmenityCodeToLabel } from "@/services/search/mapper";
import { assetPath } from "@/utils/assets";
import { renderAmenityIcon } from "@/components/hotel-details/amenityIconMapper";

/* eslint-disable @next/next/no-img-element */

interface HotelOverviewBusinessProps {
  hotel: ResponseHotelData;
}

const renderStars = (rating: number) =>
  Array.from({ length: 5 }).map((_, i) => (
    <svg
      key={i}
      className={`iconSize-15 stars-svg ${i < rating ? "fill-first" : ""}`}
      xmlns="http://www.w3.org/2000/svg"
      version="1.1"
      viewBox="0 0 105 100"
    >
      <path
        className="star"
        d="M57.8,5.9l12.1,24.5,27.1,3.9c4.9.8,6.8,6.7,3.3,10.2l-19.6,19.1,4.7,26.9c.9,4.9-4.3,8.6-8.6,6.3l-24.2-12.7-24.2,12.7c-4.4,2.3-9.4-1.4-8.7-6.3l4.7-26.9L4.8,44.6c-3.5-3.4-1.6-9.4,3.3-10.2l27.1-3.9,12.1-24.5c2.1-4.4,8.4-4.4,10.7,0h-.1Z"
      />
    </svg>
  ));

/**
 * LEIS-987 — the Business-mode hotel overview: name, stars, photos,
 * description, amenities.
 *
 * A deliberately smaller port of leisure-ui's HotelDetailsOverview: no
 * wishlist, no login-to-unlock banner, no rating/reviews card, no lightbox or
 * gallery popup (those pull in InfiniteCarousal and yet-another-react-lightbox,
 * neither of which corp has, for marketing features a Business traveller has
 * no use for).
 *
 * The photo grid DOES reuse leisure-ui's real `.hotelPhotosGrid`/`.photoCard`/
 * `.photoFrame`/`.morePhoto` vendor classes (newStyle.css, loaded by
 * <LeisureVendorAssets/> same as the rest of this page) rather than a
 * hand-rolled layout — they are scoped under `.hoteDetailSection`, which this
 * component's own wrapping <section> already carries, so reusing them is a
 * plain class swap, not new CSS. That grid is a fixed 3-cell shape (one large
 * cell spanning both rows, two small cells stacked beside it), not N
 * thumbnails — static images per cell rather than leisure-ui's hover-carousel
 * per cell (no InfiniteCarousal here either).
 */
export function HotelOverviewBusiness({ hotel }: HotelOverviewBusinessProps) {
  const images = hotel.images || [];
  const placeholder = assetPath("/assets/img/no-image.png");
  const mainImage = images[0]?.href || placeholder;
  const smallImages = images.slice(1, 3);

  const amenities: string[] = [];
  for (const amenity of hotel.amenities || []) {
    if (!amenity || typeof amenity !== "object") continue;
    Object.values(amenity).forEach((group) => {
      if (!Array.isArray(group)) return;
      group.forEach((item) => {
        if (!item || typeof item !== "object") return;
        const row = item as { description?: string; code?: string };
        const label = row.description || (row.code ? mapAmenityCodeToLabel(row.code) : "") || "";
        if (label.trim()) amenities.push(label.trim());
      });
    });
  }

  return (
    <section className="mt-4 hoteDetailSection" id="overview">
      <div className="container webNewContent">
        <div className="row">
          <div className="col-12">
            <div className="bgColor-white shadow-sm rounded-4 p-3 p-md-4 mb-0">
              <h2 className="thirdHeading fw-600 mb-0 d-flex gap-0 gap-sm-2 align-items-center">
                {formatHotelName(hotel.hotelname)}
                <span className="color-text-first selected-option fw-semibold">
                  {renderStars(Math.min(5, Math.ceil(hotel.star_rating ?? 0)))}
                </span>
              </h2>
              {hotel.city_name && <p className="text-md mb-3 mt-1">{hotel.city_name}</p>}

              <div className="hotelPhotosGrid">
                <div className="photoCard photoCard--large">
                  <div className="photoFrame">
                    <img src={mainImage} alt={hotel.hotelname} />
                    {images.length > 1 && <span className="morePhoto">+{images.length} Property Photos</span>}
                  </div>
                </div>
                {smallImages.map((img, i) => (
                  <div key={i} className="photoCard photoCard--small">
                    <div className="photoFrame">
                      <img src={img.href} alt={img.title || hotel.hotelname} />
                    </div>
                  </div>
                ))}
              </div>

              {hotel.description && (
                <div className="mt-4">
                  <h5 className="thirdHeading fw-600 mb-2">About Property</h5>
                  <p className="text-sm mb-0">{hotel.description}</p>
                </div>
              )}

              {amenities.length > 0 && (
                <div className="mt-4">
                  <h5 className="smallHeading mb-3">Amenities</h5>
                  <div className="d-flex flex-wrap column-gap-5 row-gap-3 align-items-center">
                    {amenities.slice(0, 10).map((amenity) => (
                      <div key={amenity} className="d-flex align-items-center gap-1 text-nowrap">
                        {renderAmenityIcon(amenity)}
                        <span className="text-nowrap">{amenity}</span>
                      </div>
                    ))}
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}
