'use client';

import { ChevronRight } from 'lucide-react';
import Link from 'next/link';

import { StatusBadge } from '@/components/shipments/status-badge';
import { Avatar } from '@/components/ui/avatar';
import { CopyButton } from '@/components/ui/copy-button';
import { MoneyText } from '@/components/ui/money-text';
import { countryName, flagEmoji } from '@/lib/format';
import { formatRelative, type ShipmentListItem } from '@/lib/shipments';
import { cn } from '@/lib/utils';

export function ShipmentCard({ shipment, highlighted = false }: { shipment: ShipmentListItem; highlighted?: boolean }) {
  const destination = shipment.destinationAddress;
  return (
    <article
      className={cn(
        'relative rounded-lg border border-border bg-surface p-4 shadow-xs transition-colors',
        highlighted && 'motion-safe:animate-[row-flash_800ms_ease-out]',
      )}
    >
      <div className="flex items-center justify-between gap-2">
        <span className="flex min-w-0 items-center gap-1">
          <Link href={`/shipments/${shipment.id}`} className="truncate rounded-sm font-mono text-small font-semibold text-foreground after:absolute after:inset-0 after:rounded-lg">
            {shipment.trackingNumber ?? 'Pending tracking'}
          </Link>
          {shipment.trackingNumber === undefined ? null : <CopyButton notify value={shipment.trackingNumber} label="Copy tracking number" className="relative z-[1]" />}
        </span>
        <StatusBadge status={shipment.status} />
      </div>
      <div className="mt-2.5">
        <p className="truncate text-small font-semibold">{destination.contactName}</p>
        <p className="truncate text-caption text-muted-foreground">
          <span aria-hidden="true">{flagEmoji(destination.countryCode)} </span>
          {destination.city}, {countryName(destination.countryCode)}
        </p>
      </div>
      <div className="mt-2.5 flex items-center justify-between gap-3">
        <span className="flex min-w-0 items-center gap-2 text-caption text-muted-foreground">
          {shipment.courierName === undefined ? null : <Avatar name={shipment.courierName} size="xs" shape="square" className="rounded" />}
          <span className="truncate">
            {shipment.courierName ?? 'Courier'}
            {shipment.serviceName === undefined ? '' : ` · ${shipment.serviceName}`}
          </span>
        </span>
        <span className="shrink-0 text-small font-semibold tabular-nums">
          {shipment.chargedMinor === undefined ? '—' : <MoneyText minor={shipment.chargedMinor} currency={shipment.currency} />}
        </span>
      </div>
      <div className="mt-3 flex items-center justify-between border-t border-border pt-2.5 text-caption text-muted-foreground">
        <time dateTime={shipment.createdAt}>
          {new Date(shipment.createdAt).toLocaleDateString(undefined, { month: 'short', day: 'numeric' })} · {formatRelative(shipment.createdAt)}
        </time>
        <span className="inline-flex items-center gap-0.5 font-semibold text-primary" aria-hidden="true">
          View
          <ChevronRight className="h-3.5 w-3.5" />
        </span>
      </div>
    </article>
  );
}
