"use client";

import { Users } from "lucide-react";

import type { TravellerSnapshot } from "@/services/employee-requests/employee-requests.types";
import { formatDate, formatEnumLabel } from "@/services/employee-requests/format";

/**
 * Everyone on a filed request, as the server stored them.
 *
 * Shared by the Request-submitted screen and the expanded My-requests card, so
 * a party reads the same wherever it appears.
 *
 * Passport numbers arrive already reduced to their last four digits — the full
 * number is never persisted for anyone, companions included — so this renders
 * `passportMasked` as given and has nothing to redact itself.
 */
export function TravellerList({
  travellers,
  /**
   * Is traveller 1 the person reading this?
   *
   * `isEmployee` on the snapshot means "the account holder this request was
   * filed against" — which is the TRAVELLER, not the viewer. On a colleague
   * request those are different people, and labelling the colleague "You" tells
   * the requester they are flying when they are not. False suppresses the chip;
   * the surrounding screen names both people instead.
   */
  travellerIsViewer = true,
  /** True when the caller already renders its own "Travellers" heading
   *  (e.g. a panel title with its own icon) — skips this component's
   *  otherwise-duplicate h3 instead of showing the label twice. */
  hideHeading = false,
}: {
  travellers: TravellerSnapshot[];
  travellerIsViewer?: boolean;
  hideHeading?: boolean;
}) {
  if (travellers.length === 0) return null;

  return (
    <section>
      {!hideHeading && (
        <h3 className="mb-[12px] flex items-center gap-2 text-[15px] font-bold text-[var(--fra-heading)]">
          <Users aria-hidden className="size-4 text-[var(--fra-navy-2)]" />
          Travellers
          <span className="text-xs font-medium text-[var(--fra-muted)]">({travellers.length})</span>
        </h3>
      )}
      <ul className="m-0 grid list-none gap-2 p-0">
        {travellers.map((t, i) => {
          const name = [t.firstName, t.middleName, t.lastName].filter(Boolean).join(" ");
          const facts = [
            formatEnumLabel(t.type),
            t.dateOfBirth ? formatDate(t.dateOfBirth) : null,
            t.nationality,
            // Shown only when there is one: on a domestic trip a companion may
            // legitimately have no passport recorded, and an em dash there reads
            // as missing data rather than as not required.
            t.passportMasked || null,
          ].filter(Boolean);

          return (
            <li
              key={`${t.firstName}-${t.lastName}-${i}`}
              className="flex items-start gap-[12px] rounded-xl border border-[var(--fra-border)] px-3.5 py-[12px]"
            >
              <span
                aria-hidden
                className="inline-flex size-7 shrink-0 items-center justify-center rounded-full bg-[var(--fra-navy-2)] text-xs font-bold text-white"
              >
                {i + 1}
              </span>
              <div className="min-w-0">
                <strong className="flex flex-wrap items-center gap-1.5 text-sm font-semibold text-[var(--fra-heading)]">
                  {name || `Traveller ${i + 1}`}
                  {t.isEmployee && travellerIsViewer && (
                    <span className="rounded-full bg-[var(--fra-blue-bg)] px-[7px] py-px text-[11px] font-bold text-[var(--fra-navy-2)]">
                      You
                    </span>
                  )}
                </strong>
                <small className="mt-0.5 block text-[13px] text-[var(--fra-muted)]">
                  {facts.join(" · ")}
                </small>
              </div>
            </li>
          );
        })}
      </ul>
    </section>
  );
}
