import { Suspense } from "react";
import type { Metadata } from "next";

import LeisureVendorAssets from "@/components/layout/LeisureVendorAssets";
import { HomeRouter } from "@/components/home/HomeRouter";
import { resolveDetectedOrigin } from "@/services/flights/defaultOrigin.server";

export const metadata: Metadata = {
  title: "Home · Leisure Business",
  robots: { index: false, follow: false },
};

/**
 * Rendered per request, never prerendered — same reason as the two SRP pages.
 * `FlightSearchFormV3` below reads `useSearchParams()` (it owns `bookingFor`),
 * which opts its whole subtree out of static generation.
 */
export const dynamic = "force-dynamic";

/**
 * Employee home — E0 (LEIS-1013). The admin is sent to `/admin` by `HomeRouter`.
 *
 * ── Why the vendor stylesheets are mounted here ─────────────────────────
 *
 * The search card embeds `FlightSearchFormV3` and `HotelSearchFormV3`, both
 * ported verbatim from leisure-ui and written in Bootstrap grid markup — the
 * field row is `row g-0` / `col-lg`, and the submit is `.btn-subscribe`. None
 * of that is in their colocated `fs-*` CSS; it comes from the vendored sheets.
 * So this route loads them, exactly as `/flight-request`, `/hotel-request` and
 * `/hotel-details` already do.
 *
 * Read `LeisureVendorAssets`'s own doc comment before changing this. The short
 * version: Bootstrap and Tailwind share class names at different scales and
 * Bootstrap's carry `!important`, so everything corp writes on this route uses
 * arbitrary values (`px-[16px]`, not `px-4`).
 *
 * ── Why the origin is resolved here and not in the form ─────────────────
 *
 * `resolveDetectedOrigin()` is server-only (it reads `headers()` and calls
 * IPinfo) and `react.cache`-wrapped. The flights SRP does the same thing on
 * its own cold path, and the result is the "From" field being filled in on
 * first paint rather than after a round trip.
 */
export default async function HomePage() {
  const detectedOrigin = await resolveDetectedOrigin();

  return (
    <>
      <LeisureVendorAssets />
      {/* useSearchParams() in the form needs a boundary even with
          force-dynamic — the retired SRP redirects proved that the hard way. */}
      <Suspense fallback={null}>
        <HomeRouter
          flightFormProps={{
            initialOrigin: detectedOrigin?.origin,
            initialOriginCountry: detectedOrigin?.countryCode,
          }}
        />
      </Suspense>
    </>
  );
}
