'use client';

import * as Tooltip from '@radix-ui/react-tooltip';

interface FlightNumbersProps {
  /** All flight numbers for this leg, e.g. ["LX 9853", "LX 2647"] */
  numbers: string[];
  className?: string;
}

/**
 * Renders a leg's flight number(s) on one truncated line, MMT-style.
 * When there's more than one (connecting segments under one leg), hovering
 * the truncated text reveals the full comma-separated list in a tooltip.
 */
export default function FlightNumbers({ numbers, className = 'text-xs' }: FlightNumbersProps) {
  const list = numbers.filter(Boolean);
  if (list.length === 0) return null;

  const joined = list.join(', ');

  if (list.length === 1) {
    return <div className={className}>{joined}</div>;
  }

  return (
    <Tooltip.Provider delayDuration={100}>
      <Tooltip.Root>
        <Tooltip.Trigger asChild>
          <div className={`${className} mx-auto text-truncate text-center cursor-pointer`} style={{ maxWidth: '70%' }}>
            {joined}
          </div>
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Content
            side="top"
            align="start"
            sideOffset={6}
            style={{
              zIndex: 9999,
              background: '#fff',
              border: '1px solid #e2e8f0',
              borderRadius: '8px',
              padding: '6px 10px',
              boxShadow: '0 8px 30px rgba(0,0,0,0.12)',
              fontSize: 12,
              color: '#1e293b',
              fontFamily: 'inherit',
              maxWidth: 260,
            }}
          >
            {joined}
            <Tooltip.Arrow style={{ fill: '#e2e8f0' }} />
          </Tooltip.Content>
        </Tooltip.Portal>
      </Tooltip.Root>
    </Tooltip.Provider>
  );
}
