"use client";

import { Briefcase, ShieldCheck } from "lucide-react";

import { useTravelPolicyLimits } from "@/components/flights/FarePolicyBadge";
import { gateFor } from "@/services/employee-requests/fare-policy";
import { useBusinessSession } from "@/components/employee-requests/use-session";

/**
 * "Business travel · needs approval" (PRD §6.1 / LEIS-983).
 *
 * ── Why it is its own component beside the results ───────────────────────
 *
 * The PRD puts this label "on the search box". corp's search box is
 * `FlightSearchFormV3`, 1,800 lines ported byte-identical from leisure-ui —
 * and leisure-ui has no travel policy at all, so anything added there is pure
 * divergence in the one file whose value is that it has none. `FarePolicyBadge`
 * and `BookingForBanner` both made the same call and for the same reason; this
 * sits in the slot directly under the search bar, which is where the label
 * reads from anyway.
 *
 * ── Why "needs approval" is the only part that is conditional ────────────
 *
 * The approval state is the one thing on this page that changes what the
 * employee is about to do: with it, the last step of the request is "Submit
 * for approval" and somebody else pays; without it, the last step is a card
 * payment they make themselves. That is worth stating before they have spent
 * ten minutes choosing a fare, not at step 4.
 *
 * ── Why nothing renders until the gate is known ──────────────────────────
 *
 * `gateFor` answers null while the policy is loading or if the call failed,
 * and this renders nothing in that case rather than defaulting. "Business
 * travel" on its own is a claim that no approval is needed — showing it before
 * the answer is in would tell half the companies using this the opposite of
 * the truth for a moment, on the one question that decides who pays.
 *
 * It also renders nothing with no business session: the SRP is reachable
 * signed out, and the hook is disabled there.
 *
 * Nor for an admin (LEIS-1004). Every word here is written for the employee
 * deciding what will happen to THEIR OWN request — "needs approval" vs "pay
 * now" is a real fork for them, worth knowing before they spend ten minutes
 * choosing a fare. An admin has no such fork: every booking they make is
 * direct, always, whoever it's for — so there's nothing here for this label
 * to tell them, only a "needs approval"/"Your admin reviews this trip" claim
 * that is never true for them and would be actively confusing to show while
 * they're the one booking it.
 */
export function BusinessTravelLabel() {
  const session = useBusinessSession();
  const limits = useTravelPolicyLimits();
  const gate = gateFor(limits);

  if (session?.role === "ADMIN") return null;
  if (!gate) return null;

  return (
    <div className="container mt-3">
      <span
        className="d-inline-flex align-items-center rounded-pill fw-500 gap-2 px-3 py-2 text-sm"
        style={{
          backgroundColor: "var(--fra-chip-bg, #eef2f6)",
          color: "var(--fra-ink, #0f2733)",
        }}
      >
        <Briefcase size={15} aria-hidden="true" />
        <span>Business travel</span>
        {gate.approvalRequired && (
          <>
            <span aria-hidden="true">·</span>
            <span
              className="d-inline-flex align-items-center gap-1"
              style={{ color: "var(--fra-pending, #b45309)" }}
            >
              <ShieldCheck size={15} aria-hidden="true" />
              needs approval
            </span>
          </>
        )}
      </span>
      {/* The consequence, not a restatement of the label. An employee who has
          never used Business mode has no way to know which of the two shapes
          the last step will take. */}
      <p className="mt-2 mb-0 text-sm" style={{ color: "var(--fra-muted, #64748b)" }}>
        {gate.approvalRequired
          ? "Your admin reviews this trip and pays for it once approved."
          : "You can book this trip yourself and pay with a saved company card."}
      </p>
    </div>
  );
}
