import type { HotelRequestSelection } from "./types";

/**
 * LEIS-988 — the sticky sidebar rail for the 3-step hotel request, ported
 * from flights' FlightPriceSummary: Route/Fare Type/Travelers rows, a price
 * breakdown, the Total, and the Back/Next buttons that drive every step —
 * including the last one, which is "Submit for approval" rather than
 * "Continue", exactly as FlightReviewMultiClient's STEP_META does it.
 *
 * No passenger-breakdown groups (flights' PriceBreakdown component) — a
 * hotel stay has one guest and one nightly rate, not a per-traveler-type
 * fare table, so this keeps the flatter Base Price / Taxes / Total shape
 * PriceBreakupSidebarHotel already used before this rework.
 */
export default function HotelPriceSummary({
  selection,
  stepTitle,
  nextLabel,
  totalLabel = "Total (est.)",
  prevDisabled = false,
  nextDisabled = false,
  priceNote,
  onPrev,
  onNext,
  /** LEIS-991: shown directly below Submit for approval, not as a separate
   *  block under the form — the employee's two options for the last step
   *  belong together in the one place that already drives the step. */
  showBookNow = false,
  bookNowPending = false,
  onBookNow,
  /** LEIS-1004 — see FlightPriceSummary's identical prop for why: an admin
   *  has no "Submit for approval" action at the final step, since the server
   *  already refuses it (it would file into their own approval queue). */
  hideNext = false,
  bookNowNote = "Your group doesn't require approval for this stay — pay now and skip the wait.",
  /** The default text names "your admin," which is nobody's to approve when
   *  the person looking at this is the admin. */
  repriceNote = "Re-priced when your admin approves, so the final amount can differ.",
}: {
  selection: HotelRequestSelection;
  stepTitle: string;
  nextLabel: string;
  totalLabel?: string;
  prevDisabled?: boolean;
  nextDisabled?: boolean;
  priceNote?: string;
  onPrev: () => void;
  onNext: () => void;
  showBookNow?: boolean;
  bookNowPending?: boolean;
  onBookNow?: () => void;
  hideNext?: boolean;
  bookNowNote?: string;
  repriceNote?: string;
}) {
  const basePrice = selection.nightlyRate * selection.nights;
  const taxes = selection.taxesAndFeesPerNight * selection.nights;
  const total = Math.round((basePrice + taxes) * 100) / 100;

  return (
    <div className="shadow-none position-relative h-100 sticky-payment-summary">
      <div className="accordion position-sticky top-0" id="hotelPriceSummary">
        <div className="booking-summary">
          <div className="booking-details card bgColor-white border-0 rounded-3 shadow-sm p-0">
            <div className="card-header pt-3 bgColor-white border-0">
              <h3 className="subHeading mb-0 text-lg fw-bold">{stepTitle}</h3>
            </div>

            <div className="mb-2 px-3">
              <span className="text-muted d-block text-sm">Property</span>
              <span className="fw-semibold d-block font-inter text-sm color-text-dark">
                {selection.property}
              </span>
            </div>
            <div className="mb-2 px-3">
              <span className="text-muted d-block text-sm">Room &amp; Rate</span>
              <span className="fw-semibold d-block font-inter text-sm color-text-dark">
                {selection.roomType}
                {selection.rateType ? ` · ${selection.rateType}` : ""}
              </span>
            </div>
            <div className="px-3">
              <span className="text-muted d-block text-sm">Stay</span>
              <span className="fw-semibold d-block font-inter text-sm color-text-dark">
                {selection.nights} night{selection.nights === 1 ? "" : "s"} · 1 room · {selection.adults ?? 1} adult
                {(selection.adults ?? 1) === 1 ? "" : "s"}
              </span>
            </div>

            <div className="card-body bgColor-white">
              <h4 className="text-md fw-700 color-text-dark mb-2">Price breakdown</h4>
              <ul className="d-flex justify-content-between passenger-price-summary ps-0 py-1 mb-0">
                <li className="passenger-number text-sm">
                  Base Price
                  <br />
                  <small>
                    [ 1 Room x {selection.nights} night{selection.nights === 1 ? "" : "s"} ]
                  </small>
                </li>
                <li className="passenger-fare text-sm text-end">
                  {selection.currency} {basePrice.toFixed(2)}
                </li>
              </ul>
              <ul className="d-flex justify-content-between passenger-price-summary ps-0 py-1 mb-0">
                <li className="passenger-number text-sm">Taxes &amp; Service Fees</li>
                <li className="passenger-fare text-sm text-end">
                  {selection.currency} {taxes.toFixed(2)}
                </li>
              </ul>
            </div>

            <div className="card-footer bgColor-white pt-1 border-0 border-top">
              <ul className="d-flex justify-content-between passenger-price-summary passenger-price-summary-total ps-0 py-1 mb-0">
                <li className="passenger-number text-md fw-semibold">{totalLabel}</li>
                <li className="passenger-fare text-md text-right fw-semibold">
                  {selection.currency} {total.toFixed(2)}
                </li>
              </ul>
              {priceNote ? (
                <small className="text-xs color-text-second d-block fw-600">{priceNote}</small>
              ) : null}
              <small className="text-xs">{repriceNote}</small>
            </div>

            <div className="p-3">
              <button
                type="button"
                className="buttons btnColor-first rounded-2 mt-3 w-100 text-md"
                disabled={prevDisabled}
                onClick={onPrev}
              >
                Back
              </button>
              {!hideNext && (
                <button
                  type="button"
                  className="buttons btnColor-second rounded-2 mt-3 w-100 text-md"
                  disabled={nextDisabled}
                  onClick={onNext}
                >
                  {nextDisabled ? "Sending…" : nextLabel}
                </button>
              )}
              {showBookNow && (
                <>
                  <button
                    type="button"
                    className="buttons btnColor-second rounded-2 mt-3 w-100 text-md"
                    disabled={bookNowPending}
                    onClick={onBookNow}
                  >
                    <i className="bi bi-wallet2 me-2" aria-hidden="true" />
                    {bookNowPending ? "Please wait…" : "Book Now"}
                  </button>
                  <small className="text-xs d-block mt-2">{bookNowNote}</small>
                </>
              )}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
