import type { ReactNode } from 'react';
import type { FlightAmenityType } from './types';

const STROKE = '#6b7280';

const SVG_PROPS = {
  xmlns: 'http://www.w3.org/2000/svg',
  width: 15,
  height: 15,
  viewBox: '0 0 24 24',
  fill: 'none',
  strokeWidth: 1.9,
  strokeLinecap: 'round' as const,
  strokeLinejoin: 'round' as const,
};

/**
 * Icon for the full amenity list. Amadeus tags most fare perks as
 * `BRANDED_FARES` regardless of what they are ("Seat Selection", "Mileage
 * Accrual", "Changeable Ticket"…), so the icon is resolved from the *label*
 * first (keyword rules), then the AmenityType, then a neutral sparkle — that
 * way each perk gets a real, recognisable glyph instead of one generic badge.
 * Always one neutral grey (matching the inline pill) — "chargeable" is spelled
 * out next to the label, not colour-coded.
 */
type IconKey =
  | 'wifi' | 'screen' | 'meal' | 'cup' | 'power' | 'seat' | 'layout' | 'lounge'
  | 'award' | 'upgrade' | 'calendar' | 'refund' | 'priority' | 'leaf' | 'bus'
  | 'medical' | 'paw' | 'hourglass' | 'bag' | 'bell' | 'userCheck' | 'package' | 'sparkle';

const GLYPH: Record<IconKey, ReactNode> = {
  wifi: (<><path d="M12 20h.01" /><path d="M2 8.82a15 15 0 0 1 20 0" /><path d="M5 12.86a10 10 0 0 1 14 0" /><path d="M8.5 16.43a5 5 0 0 1 7 0" /></>),
  screen: (<><rect width="20" height="14" x="2" y="3" rx="2" /><line x1="8" x2="16" y1="21" y2="21" /><line x1="12" x2="12" y1="17" y2="21" /></>),
  meal: (<><path d="M3 2v7c0 1.1.9 2 2 2h4a2 2 0 0 0 2-2V2" /><path d="M7 2v20" /><path d="M21 15V2a5 5 0 0 0-5 5v6c0 1.1.9 2 2 2h3Zm0 0v7" /></>),
  cup: (<><path d="M18 8h1a4 4 0 0 1 0 8h-1" /><path d="M2 8h16v9a4 4 0 0 1-4 4H6a4 4 0 0 1-4-4Z" /><line x1="6" x2="6" y1="2" y2="4" /><line x1="10" x2="10" y1="2" y2="4" /><line x1="14" x2="14" y1="2" y2="4" /></>),
  power: (<><path d="M12 22v-5" /><path d="M9 8V2" /><path d="M15 8V2" /><path d="M18 8v5a4 4 0 0 1-4 4h-4a4 4 0 0 1-4-4V8Z" /></>),
  seat: (<><path d="M19 9V6a2 2 0 0 0-2-2H7a2 2 0 0 0-2 2v3" /><path d="M3 16a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-2a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2Z" /><path d="M5 18v2" /><path d="M19 18v2" /></>),
  layout: (<><rect width="7" height="7" x="3" y="3" rx="1" /><rect width="7" height="7" x="14" y="3" rx="1" /><rect width="7" height="7" x="14" y="14" rx="1" /><rect width="7" height="7" x="3" y="14" rx="1" /></>),
  lounge: (<><path d="M20 9V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v3" /><path d="M4 11v5a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-5a2 2 0 0 0-4 0v2H8v-2a2 2 0 0 0-4 0Z" /><path d="M4 18v2" /><path d="M20 18v2" /></>),
  award: (<><path d="m15.477 12.89 1.515 8.526a.5.5 0 0 1-.81.47l-3.58-2.687a1 1 0 0 0-1.197 0l-3.586 2.686a.5.5 0 0 1-.81-.469l1.514-8.526" /><circle cx="12" cy="8" r="6" /></>),
  upgrade: (<><path d="m18 15-6-6-6 6" /><path d="M12 21V9" /></>),
  calendar: (<><path d="M8 2v4" /><path d="M16 2v4" /><rect width="18" height="18" x="3" y="4" rx="2" /><path d="M3 10h18" /><path d="m9 16 2 2 4-4" /></>),
  refund: (<><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" /><path d="M3 3v5h5" /><path d="M12 7v5l3 2" /></>),
  priority: (<><path d="M13 2 3 14h9l-1 8 10-12h-9l1-8Z" /></>),
  leaf: (<><path d="M11 20A7 7 0 0 1 9.8 6.1C15.5 5 17 4.48 19 2c1 2 2 4.18 2 8 0 5.5-4.78 10-10 10Z" /><path d="M2 21c0-3 1.85-5.36 5.08-6C9.5 14.52 12 13 13 12" /></>),
  bus: (<><path d="M8 6v6" /><path d="M15 6v6" /><path d="M2 12h19.6" /><path d="M18 18h3s.5-1.7.8-2.8c.1-.4.2-.8.2-1.2s-.1-.8-.2-1.2l-1.4-5C20.1 6.8 19.1 6 18 6H4a2 2 0 0 0-2 2v10h3" /><circle cx="7" cy="18" r="2" /><path d="M9 18h5" /><circle cx="16" cy="18" r="2" /></>),
  medical: (<><path d="M22 12h-2.48a2 2 0 0 0-1.93 1.46l-2.35 8.36a.25.25 0 0 1-.48 0L9.24 2.18a.25.25 0 0 0-.48 0l-2.35 8.36A2 2 0 0 1 4.49 12H2" /></>),
  paw: (<><circle cx="11" cy="4" r="2" /><circle cx="18" cy="8" r="2" /><circle cx="20" cy="16" r="2" /><path d="M9 10a5 5 0 0 1 5 5v3.5a3.5 3.5 0 0 1-6.84 1.045Q6.52 17.48 4.46 16.84A3.5 3.5 0 0 1 5.5 10Z" /></>),
  hourglass: (<><path d="M5 22h14" /><path d="M5 2h14" /><path d="M17 22v-4.172a2 2 0 0 0-.586-1.414L12 12l-4.414 4.414A2 2 0 0 0 7 17.828V22" /><path d="M7 2v4.172a2 2 0 0 0 .586 1.414L12 12l4.414-4.414A2 2 0 0 0 17 6.172V2" /></>),
  bag: (<><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z" /><path d="M3 6h18" /><path d="M16 10a4 4 0 0 1-8 0" /></>),
  bell: (<><path d="M10.268 21a2 2 0 0 0 3.464 0" /><path d="M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326" /></>),
  userCheck: (<><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2" /><circle cx="9" cy="7" r="4" /><path d="m16 11 2 2 4-4" /></>),
  package: (<><path d="M16.5 9.4 7.55 4.24" /><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z" /><path d="M3.29 7 12 12l8.71-5" /><path d="M12 22V12" /></>),
  sparkle: (<><path d="M9.94 15.5A2 2 0 0 0 8.5 14.06l-6.14-1.58a.5.5 0 0 1 0-.96L8.5 9.94A2 2 0 0 0 9.94 8.5l1.58-6.14a.5.5 0 0 1 .96 0L14.06 8.5A2 2 0 0 0 15.5 9.94l6.14 1.58a.5.5 0 0 1 0 .96L15.5 14.06a2 2 0 0 0-1.44 1.44l-1.58 6.14a.5.5 0 0 1-.96 0z" /></>),
};

const TYPE_ICON: Partial<Record<FlightAmenityType, IconKey>> = {
  WIFI: 'wifi',
  ENTERTAINMENT: 'screen',
  MEAL: 'meal',
  BEVERAGE: 'cup',
  POWER: 'power',
  SEAT: 'seat',
  PRE_RESERVED_SEAT: 'seat',
  LAYOUT: 'layout',
  LOUNGE: 'lounge',
  FREQUENT_FLYER: 'award',
  UPGRADES: 'upgrade',
  CARBON_OFFSET: 'leaf',
  GROUND: 'bus',
  MEDICAL: 'medical',
  PETS: 'paw',
  STANDBY: 'hourglass',
  STORE: 'bag',
  TRAVEL_SERVICES: 'bell',
  UNACCOMPANIED_TRAVEL: 'userCheck',
  UNACCOMPANIED_TRAVEL_UNESCORTED: 'userCheck',
  BUNDLED_SERVICE: 'package',
};

/** Ordered — first match wins. */
const LABEL_RULES: Array<[RegExp, IconKey]> = [
  [/wi-?fi|internet/, 'wifi'],
  [/entertain|streaming|on-?demand|screen|movie|\bife\b/, 'screen'],
  [/power|usb|socket|outlet/, 'power'],
  [/lounge/, 'lounge'],
  [/\bseat\b/, 'seat'],
  [/layout|configuration|\d-\d/, 'layout'],
  [/beverage|drink|water|juice|coffee|\btea\b|refreshment/, 'cup'],
  [/meal|food|dining|breakfast|lunch|dinner|catering|snack/, 'meal'],
  [/mile|accrual|frequent ?flyer|points|award/, 'award'],
  [/upgrade/, 'upgrade'],
  [/change|rebook|revalidat/, 'calendar'],
  [/refund/, 'refund'],
  [/priorit|fast ?track|go ?show|expedit/, 'priority'],
  [/carbon|co2|emission|offset|sustainab/, 'leaf'],
  [/\bpet\b|animal/, 'paw'],
  [/medical|wheelchair|special ?assist/, 'medical'],
  [/standby/, 'hourglass'],
  [/shop|store|duty ?free|boutique/, 'bag'],
  [/ground|transfer|shuttle|chauffeur|\blimo\b/, 'bus'],
  [/unaccompanied|\bminor\b/, 'userCheck'],
  [/baggage|check(ed| )?bag|cabin bag/, 'bag'],
];

function resolveIcon(type: FlightAmenityType, label: string): IconKey {
  const l = label.toLowerCase();
  for (const [re, key] of LABEL_RULES) if (re.test(l)) return key;
  return TYPE_ICON[type] ?? 'sparkle';
}

export default function FlightAmenityDetailIcon({
  type,
  label = '',
}: {
  type: FlightAmenityType;
  label?: string;
}) {
  return (
    <svg {...SVG_PROPS} stroke={STROKE} className="flex-shrink-0" aria-hidden>
      {GLYPH[resolveIcon(type, label)]}
    </svg>
  );
}
