"use client";

import { Luggage, Users } from "lucide-react";

import AirlineLogo from "@/components/flights/AirlineLogo";

import type { FlightOfferPayload } from "@/services/employee-requests/employee-requests.types";
import { formatMoney, formatTripDates } from "@/services/employee-requests/format";

/**
 * The flight the employee is requesting, pinned above the form.
 *
 * Every element on the navy band states its own colour rather than inheriting
 * one. The overlay is portalled to document.body, and on /srp that document
 * carries leisure-ui's vendored stylesheets, which break colour inheritance
 * here — the carrier badge and the route arrow both rendered in
 * `--color-content` (#474646) on navy, i.e. invisible. The design's own
 * stylesheet sets `color: #fff` on each of these for the same reason.
 *
 * It stays visible the whole way down the overlay because everything below it —
 * the justification, the cost, the policy verdict — is an argument about *this*
 * itinerary, and the one question an approver asks back is "which flight?".
 *
 * Rendered from the built payload rather than the display `FlightData`: it is
 * what will actually be sent, so if the two ever disagree, the employee should
 * see what the request says, not what the search card said.
 */
export function SelectedFlightBand({
  offer,
  times,
}: {
  offer: FlightOfferPayload;
  /** "10:45" / "19:33" local clock times for the outbound, when known. */
  times?: { depart?: string; arrive?: string };
}) {
  const travellers = offer.travellers ?? 1;
  const stops = offer.stops ?? 0;
  const duration = offer.durationMinutes
    ? `${Math.floor(offer.durationMinutes / 60)}h ${offer.durationMinutes % 60}m`
    : null;

  const line2 = [
    offer.airline,
    offer.flightNumbers,
    stops === 0 ? "Non-stop" : `${stops} stop${stops > 1 ? "s" : ""}`,
    duration,
  ]
    .filter(Boolean)
    .join(" · ");

  const cabinLabel = offer.cabin
    .toLowerCase()
    .replace(/_/g, " ")
    .replace(/\b\w/g, (c) => c.toUpperCase());

  // LEIS-966. Absent on a hotel request and on any offer built before step 3
  // existed, so every read is optional — the band simply omits the line.
  const fareOption = offer.addOns?.fareOption?.trim() || null;
  const extraBags = offer.addOns?.extraBags ?? 0;
  const bagCost =
    offer.addOns?.extraBagsCost && offer.addOns.extraBagsCost > 0
      ? formatMoney(offer.addOns.extraBagsCost, offer.currency)
      : null;

  return (
    <div className="mt-[16px] mb-1 flex flex-col gap-[12px] rounded-xl bg-[linear-gradient(120deg,var(--fra-navy)_0%,var(--fra-navy-2)_100%)] px-[16px] py-3.5 text-white sm:flex-row sm:items-center sm:gap-3.5">
      {/* The same S3 logo the SRP cards use, through the same component — it is
          the only thing that handles a carrier with no object in the bucket
          (S3 answers 403, not 404, so a missing logo is only knowable from the
          failed request). On a white tile because the logos are coloured
          artwork on transparency and would disappear into the navy. */}
      <span
        aria-hidden
        className="inline-flex size-12 shrink-0 items-center justify-center rounded-xl bg-white p-1.5 shadow-[0_0_0_2px_rgba(255,255,255,0.25)]"
      >
        <AirlineLogo
          code={offer.carrierCode}
          name={offer.airline ?? undefined}
          className="size-full object-contain"
        />
      </span>

      <div className="min-w-0 flex-1">
        <strong className="block text-base text-white">
          {offer.origin}
          {times?.depart ? ` ${times.depart}` : ""}
          <span aria-hidden className="text-white">
            {" → "}
          </span>
          <span className="sr-only"> to </span>
          {offer.destination}
          {times?.arrive ? ` ${times.arrive}` : ""}
        </strong>
        {line2 && <span className="mt-0.5 block text-[13px] text-white/80">{line2}</span>}
        <span className="mt-0.5 block text-[13px] text-white/80">
          {formatTripDates(offer)} · {cabinLabel}
          {/* LEIS-966: "The summary banner includes the fare option and extra
              bags." The fare BRAND matters here in a way the cabin alone does
              not — Economy Light and Economy Flex differ on exactly the
              baggage and change rules an approver is deciding about. */}
          {fareOption ? ` · ${fareOption}` : ""}
        </span>
        {extraBags > 0 ? (
          <span className="mt-0.5 flex items-center gap-1.5 text-[13px] text-white/80">
            <Luggage aria-hidden className="size-3.5" />
            {extraBags} extra checked bag{extraBags === 1 ? "" : "s"}
            {bagCost ? ` · ${bagCost}` : ""}
          </span>
        ) : null}
        <span className="mt-0.5 flex items-center gap-1.5 text-[13px] text-white/80">
          <Users aria-hidden className="size-3.5" />
          {travellers} traveller{travellers === 1 ? "" : "s"}
        </span>
      </div>

      <div className="shrink-0 text-left sm:text-right">
        <div className="text-2xl font-bold text-white">
          {formatMoney(offer.fare, offer.currency)}
        </div>
        <small className="block text-xs font-medium text-white/70">
          {offer.tripType === "ROUND_TRIP"
            ? "round trip"
            : offer.tripType === "MULTI_CITY"
              ? "multi-city"
              : "one way"}
          {/* The fare covers everyone, so say so rather than letting it read as
              a per-person price. */}
          {travellers > 1 ? ` · ${travellers} travellers` : ""}
        </small>
      </div>
    </div>
  );
}
