"use client";

import { FarePolicyBadge } from "@/components/flights/FarePolicyBadge";
import {
  evaluateRequestOffer,
  type TravelPolicyLimits,
} from "@/services/employee-requests/fare-policy";

/**
 * The travel-policy verdict on step 1 (LEIS-981: the check runs on the
 * "Request steps (Review Flight, Request approval page)", not only the last
 * one).
 *
 * Why it belongs here and not only on step 4: the traveller has by this point
 * left the search page, and everything that follows — traveller details, bags,
 * a card number — is work. Finding out at the end that the trip is three flags
 * over the group's limit is finding out too late to pick a different flight
 * cheaply. The flags are the same ones the SRP card carried, restated against
 * the PRICED offer, which is the first point at which the real total is known.
 *
 * Advisory and never blocking. PRD §7 and the parent ticket are explicit that
 * flight policy "never blocks anything: out-of-policy flights and fares stay
 * visible and requestable or bookable". So this explains, and Continue stays
 * exactly as enabled as it was.
 *
 * Renders nothing with no policy to apply — signed out, still loading, or the
 * call failed — for the reason `FlightResultPolicyBadge` documents: an
 * unearned green badge reads as a checked claim.
 */
export default function ReviewPolicyPanel({
  offer,
  currency,
  limits,
}: {
  offer: { cabin?: string; fare?: number; departDate?: string } | null;
  currency?: string;
  limits: TravelPolicyLimits | null;
}) {
  if (!limits || !offer) return null;

  const verdict = evaluateRequestOffer({ offer, currency, limits });
  const groupName = limits.group?.name;

  return (
    <div className="bgColor-blue-50 border border-color-blue-200 rounded-3 p-3 p-md-4 mt-4">
      <div className="d-flex align-items-start justify-content-between gap-3 flex-wrap">
        <div>
          <h3 className="text-lg color-text-same fw-700 mb-1">Travel policy</h3>
          <p className="text-sm color-text-same mb-0">
            {groupName
              ? `Checked against your ${groupName} policy for this trip.`
              : "Checked against your travel policy for this trip."}
          </p>
        </div>
        {/* The count only — the flags are listed below, where there is room
            to read them. Shown twice is how the first cut of this read. */}
        <FarePolicyBadge verdict={verdict} showFlags={false} />
      </div>

      {!verdict.inPolicy && (
        <>
          <ul className="list-style-disc mb-0 mt-3 d-flex flex-column gap-2 ps-3">
            {verdict.flags.map((flag) => (
              <li key={flag.rule} className="text-sm color-text-same">
                {flag.label}
              </li>
            ))}
          </ul>
          {/* Said plainly, because the alternative is a traveller abandoning a
              trip they are entitled to book. */}
          <p className="text-xs color-text-same mb-0 mt-3">
            You can still continue — flags don&rsquo;t block the booking, they go to your admin
            with it.
          </p>
        </>
      )}
    </div>
  );
}
