'use client';

import type { FareFeatureIcon, FareOption } from './types';

/**
 * What one fare card lists: its inclusions, in labelled blocks.
 *
 * Extracted because the same markup was written out three times — twice in
 * FlightFaresModal and once in FlightFareOptions — so a change to how a fare
 * reads had to be made in three places and stayed right in only as many as
 * someone remembered.
 *
 * Falls back to the flat `features` list when a fare carries no groups, so a
 * cached or older payload still renders everything it has.
 */
export default function FareFeatureList({
  fare,
  firstGroupOnly = false,
}: {
  fare: FareOption;
  /**
   * Show only the leading group — Baggage, in practice.
   *
   * Set by the fare card on a phone, where a dozen inclusions per card turns
   * comparing two fares into a scroll past one of them. Truncating by group
   * rather than by item count keeps every heading with its own contents; a
   * flat "first three rows" would orphan a heading from the list it labels.
   */
  firstGroupOnly?: boolean;
}) {
  const allGroups =
    fare.featureGroups && fare.featureGroups.length > 0
      ? fare.featureGroups
      : fare.features.length
        ? [{ title: '', features: fare.features }]
        : [];
  const groups = firstGroupOnly ? allGroups.slice(0, 1) : allGroups;

  if (groups.length === 0 && !fare.sameAs) return null;

  return (
    <div className="flight-fare-features mb-3 text-start">
      {groups.map((group) => (
        <div key={group.title || 'all'} className="flight-fare-group">
          {/* No `text-xs` on the heading: that utility is `font-size: …!important`
              in leisure-new-style.css, so it would render the heading at exactly
              the size of the items it labels — which is what made these blocks
              read as one run-on list. The size lives in flights.css instead. */}
          {group.title ? (
            <div className="flight-fare-group-title fw-600 text-uppercase color-text-grey">
              {group.title}
            </div>
          ) : null}
          <ul className="flight-fare-points list-unstyled mb-0">
            {group.features.map((feature, idx) => (
              <li key={`${group.title}-${idx}`}>
                <div className="d-flex align-items-start gap-2">
                  <FeatureIcon icon={feature.icon} />
                  <span className="text-xs color-text-grey">{feature.label}</span>
                </div>
              </li>
            ))}
          </ul>
        </div>
      ))}

      {/* Only when Amadeus genuinely returns the same inclusions for both
          fares — see markIndistinguishableFares. Saying it plainly beats
          leaving someone to compare two identical columns and wonder what
          the extra money buys. */}
      {fare.sameAs ? (
        <p className="flight-fare-same-as color-text-grey mb-0">
          Same baggage and benefits as {fare.sameAs}. The airline does not
          publish what else differs until checkout.
        </p>
      ) : null}
    </div>
  );
}

/**
 * The tick / cross / bag beside each inclusion.
 *
 * All three are the same 15px box so the labels start on one line rather than
 * stepping in and out — the ticks and crosses used to be 12px against the
 * bag's 15px, which is what made the column look ragged.
 *
 * Strokes are heavier than the 2 used elsewhere: at this size the glyph is
 * only a few pixels of line, and whether a fare *includes* something or
 * *charges* for it is the single thing a customer is reading these cards to
 * find out. It has to survive a glance.
 */
function FeatureIcon({ icon }: { icon: FareFeatureIcon }) {
  if (icon === 'baggage') {
    return (
      <svg className="iconSize-15 stroke-black flex-shrink-0" width="15" height="15" viewBox="0 0 24 24" fill="none" strokeWidth="2.25" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <path d="M6 20a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2" />
        <path d="M8 18V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v14" />
        <path d="M10 20h4" />
        <circle cx="16" cy="20" r="2" />
        <circle cx="8" cy="20" r="2" />
      </svg>
    );
  }
  if (icon === 'check') {
    return (
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.75" strokeLinecap="round" strokeLinejoin="round" className="iconSize-15 stroke-green-600 flex-shrink-0" aria-hidden="true">
        <path d="M20 6 9 17l-5-5" />
      </svg>
    );
  }
  if (icon === 'cross') {
    return (
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.75" strokeLinecap="round" strokeLinejoin="round" className="iconSize-15 stroke-red-400 flex-shrink-0" aria-hidden="true">
        <path d="M18 6 6 18" />
        <path d="m6 6 12 12" />
      </svg>
    );
  }
  return (
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" className="iconSize-15 stroke-gray-400 flex-shrink-0" aria-hidden="true">
      <circle cx="12" cy="12" r="10" />
      <path d="M12 16v-4" />
      <path d="M12 8h.01" />
    </svg>
  );
}
