"use client";

import { useState } from "react";

import type { FlightSearchFormV3Props } from "@/components/flights/v3/FlightSearchFormV3";
import { useAdminSession } from "@/components/travel-policy/useAdminSession";
import { HomeHero } from "./HomeHero";
import { HomeSearchPanel, type HomeSearchTab } from "./HomeSearchPanel";
import { AwaitingApproval } from "./panels/AwaitingApproval";
import { CompanyGlance } from "./panels/CompanyGlance";
import { RecentBookings } from "./panels/RecentBookings";

/**
 * Admin home — D0 (LEIS-1013).
 *
 * The admin's landing page: the same search, but booking for anyone in the
 * company and paying directly. Waiting for your approval, Your recent
 * bookings, Company at a glance and the quick links arrive in LEIS-1019 /
 * 1020 / 1021; this is the hero, the search and the chips.
 *
 * ── Why the chips always say "No approval" here ─────────────────────────
 *
 * PRD §8.6: the admin books directly on the company card, with no approval
 * round-trip, whichever employee they are booking for. The traveller's group
 * still decides the POLICY FLAGS on the fare they pick — that is what the
 * panel below will show — but it never decides whether the admin needs
 * somebody's permission, because there is nobody above them to ask.
 *
 * So D0 does not load a policy at all. `useHomePolicy` exists for E0, where
 * the answer genuinely varies by group and tab; here it would be a round trip
 * whose result could not change what is shown.
 *
 * The gate is enforced server-side regardless: every admin route is
 * `requireBusinessAdmin`, and `useAdminSession` only avoids rendering a page
 * whose every request would answer 403.
 */
export function AdminHome({ flightFormProps }: { flightFormProps?: FlightSearchFormV3Props }) {
  const { token, status } = useAdminSession();
  const [tab, setTab] = useState<HomeSearchTab>("flights");

  // `checking` is the hydration pass, before localStorage has been read.
  // Rendering the page then would flash it at a signed-out visitor;
  // `useAdminSession` redirects on its own once there is a real answer.
  if (status !== "admin") return null;

  return (
    <>
      <HomeHero
        title="Admin · Book for yourself or any employee"
        subtitle="Search as normal. Bookings you make are charged to the company card straight away — no approval needed."
      >
        <HomeSearchPanel
          tab={tab}
          onTabChange={setTab}
          approvalRequired={false}
          policyKnown
          flightFormProps={flightFormProps}
        />
      </HomeHero>

      {/*
       * What needs deciding takes the wide column; the company's own numbers
       * and the shortcuts sit beside it. On a phone the grid collapses in this
       * order, which is the right one: approvals are the only thing here that
       * somebody is waiting on.
       */}
      <div
        data-corp-home
        className="mx-auto w-full max-w-[1200px] px-[16px] pt-[32px] pb-[48px]"
      >
        <div className="grid gap-[24px] lg:grid-cols-[minmax(0,1fr)_340px]">
          <div className="grid content-start gap-[24px]">
            <AwaitingApproval token={token!} />
            <RecentBookings token={token!} />
          </div>
          <div className="grid content-start gap-[24px]">
            <CompanyGlance token={token!} />
          </div>
        </div>
      </div>
    </>
  );
}
