"use client";

import FlightSearchFormV3, {
  type FlightSearchFormV3Props,
} from "@/components/flights/v3/FlightSearchFormV3";
import HotelSearchFormV3 from "@/components/search/v3/HotelSearchFormV3";
import { FlightsTabIcon, HotelsTabIcon } from "@/components/home/v3/icons";
import { StatusChips, StatusChipsSkeleton } from "@/components/booking-for/StatusChips";

import "./home-search.css";

export type HomeSearchTab = "flights" | "hotels";

/**
 * The notched search panel on the home hero (LEIS-1015, reskinned by LEIS-1046).
 *
 * ── Why the ported forms rather than new ones ───────────────────────────
 *
 * `FlightSearchFormV3` was written for exactly this: its own doc comment says
 * "one component serves both the home page hero and the SRP", and
 * `isResultsPage` already gates every auto-search path, so off `/srp/flights`
 * it navigates once on submit instead of searching as you type. Rebuilding a
 * second flight form in Tailwind would mean a second airport autocomplete, a
 * second date picker, a second travellers panel and a second URL builder — and
 * the first one to drift would silently send a different search than the SRP.
 *
 * ── Why this is the SRP's chrome and not a Tailwind card ────────────────
 *
 * This used to be a white `--fra-surface` card with a navy segmented control
 * for the two tabs, and `home-search.css` spent most of its length UNDOING the
 * decoration `.search-form-wrap` brings so the two cards did not nest. The
 * reskin runs the other way: the markup below is `FlightSearchPanelV3`'s,
 * element for element — `.search-panel-container`, the `.shape-element` notch,
 * `.hero-tabs`, `.search-form-wrap` — so the panel IS the vendored card rather
 * than a second one drawn around it. What corp still writes itself uses
 * arbitrary values (`gap-[8px]`, never `gap-2`): Bootstrap ships those same
 * names at a different scale with `!important`, which no cascade layer beats.
 *
 * The one addition to the SRP's row is the status chips. They answer "who pays
 * for this trip", which the SRP does not have to because by then the traveller
 * is already looking at `BusinessTravelLabel` above the results.
 *
 * ── Why Cruises is not a third tab ──────────────────────────────────────
 *
 * The SRP band has one because leisure.com sells cruises. Business mode does
 * not: there is no cruise policy, no cruise request and nothing for an
 * approver to approve, so a tab here would lead somewhere that cannot serve a
 * corporate traveller.
 *
 * ── Why the tab is local state and not the URL ──────────────────────────
 *
 * Flights is the default on every arrival, by the ticket: "After sign-up,
 * account setup and every sign-in, everyone lands on their home page with the
 * Flights tab selected." A URL-backed tab would survive a reload and quietly
 * break that. The traveller is the opposite case — it IS in the URL, because
 * `FlightSearchFormV3` owns `bookingFor` and carries it into the search.
 */
export function HomeSearchPanel({
  tab,
  onTabChange,
  approvalRequired,
  policyKnown,
  flightFormProps,
}: {
  /**
   * Controlled, because the tab is not this card's private business: "Your
   * travel policy" beside it shows the flight rules or the hotel rules
   * depending on it, and the chips above change with it. The panel that owns
   * both reads the same value.
   */
  tab: HomeSearchTab;
  onTabChange: (tab: HomeSearchTab) => void;
  /** The selected traveller's gate for the current tab. */
  approvalRequired: boolean;
  /** False while the policy is still loading — the chips hold their space. */
  policyKnown: boolean;
  flightFormProps?: FlightSearchFormV3Props;
}) {
  return (
    <div className="search-panel-container text-center text-md-start h-auto mb-0">
      {/*
       * The white notch that joins the selected tab to the panel below.
       * Absolutely positioned at `z-index: -1` inside this container, so it
       * paints behind the tabs but still above the banner. Copied verbatim
       * from `FlightSearchPanelV3` — the two must trace the same outline.
       */}
      <div className="shape-element">
        <svg
          className="fill-white"
          width={406}
          height={25}
          viewBox="0 0 406 25"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
          aria-hidden
        >
          <path
            d="M0 24C0 10.7452 10.7452 2.77559e-06 24 2.6702e-06L347.101 1.01301e-07C354.84 3.97714e-08 361.844 4.58429 364.94 11.6766C367.958 18.5881 374.696 23.1346 382.235 23.3455L405.132 23.986C405.41 23.9938 405.632 24.2216 405.632 24.5H0V24Z"
            fill="white"
          />
        </svg>
      </div>

      <div className="corp-home-tabrow">
        <div
          className="hero-tabs d-inline-flex align-items-center gap-2 mb-2 mb-md-0 me-auto me-md-0 ms-auto ms-md-3"
          role="tablist"
          aria-label="What are you booking?"
        >
          <TabButton
            active={tab === "hotels"}
            onClick={() => onTabChange("hotels")}
            icon={<HotelsTabIcon />}
            label="Hotels"
          />
          <TabButton
            active={tab === "flights"}
            onClick={() => onTabChange("flights")}
            icon={<FlightsTabIcon />}
            label="Flights"
          />
        </div>

        <div className="corp-home-chips">
          {policyKnown ? (
            <StatusChips approvalRequired={approvalRequired} />
          ) : (
            <StatusChipsSkeleton />
          )}
        </div>
      </div>

      {/*
       * Both forms stay mounted is NOT what happens here, and deliberately:
       * each owns a long-lived draft (an itinerary, a party, a date range)
       * and `HotelSearchFormV3` re-reads the URL on every `searchParams`
       * change. Rendering only the active one keeps one source of truth for
       * "what am I about to search for" and matches what the tab claims.
       *
       * `HotelSearchFormV3`'s `embedded` branch already returns its own
       * `.search-form-wrap > .search-tab-panel` pair, which is why only the
       * flight branch writes one — the same split `FlightSearchPanelV3` makes.
       */}
      {tab === "flights" ? (
        <div className="search-form-wrap text-start px-2">
          <div className="search-tab-panel" data-panel="flights">
            <FlightSearchFormV3 {...flightFormProps} />
          </div>
        </div>
      ) : (
        <HotelSearchFormV3 embedded />
      )}
    </div>
  );
}

/**
 * One product tab.
 *
 * A `<button>` rather than the SRP's mixture of a button and two links,
 * because here the tab swaps the form in place instead of navigating. Every
 * class on it is the vendor's, so it renders identically to the SRP's: the
 * selected one is the solid white pill the notch runs into.
 */
function TabButton({
  active,
  onClick,
  icon,
  label,
}: {
  active: boolean;
  onClick: () => void;
  icon: React.ReactNode;
  label: string;
}) {
  return (
    <button
      type="button"
      role="tab"
      aria-selected={active}
      onClick={onClick}
      className={`hero-tab buttons py-1 px-2 bg-transparent text-md d-flex align-items-center gap-2${
        active ? " active" : ""
      }`}
    >
      {icon}
      {label}
    </button>
  );
}
