import { Mail, MapPin, PackageOpen, Phone, Truck, type LucideIcon } from 'lucide-react';

import { Card } from '@/components/ui/card';
import { CopyButton } from '@/components/ui/copy-button';
import { countryName, flagEmoji } from '@/lib/format';
import type { ShipmentAddress } from '@/lib/shipments';

function ContactLine({ icon: Icon, value, label }: { icon: LucideIcon; value: string; label: string }) {
  return (
    <p className="group/line flex items-center gap-1.5 text-small text-muted-foreground">
      <Icon className="h-3.5 w-3.5 shrink-0" aria-hidden="true" />
      <span className="min-w-0 truncate">{value}</span>
      <CopyButton notify value={value} label={`Copy ${label}`} className="h-6 w-6 opacity-0 transition-opacity focus-visible:opacity-100 group-hover/line:opacity-100 [@media(hover:none)]:opacity-100" />
    </p>
  );
}

function Party({ title, icon: Icon, address }: { title: string; icon: LucideIcon; address: ShipmentAddress }) {
  return (
    <div className="flex gap-3">
      <span className="relative z-[1] grid h-9 w-9 shrink-0 place-items-center rounded-lg bg-primary-soft text-primary ring-4 ring-surface">
        <Icon className="h-[18px] w-[18px]" aria-hidden="true" />
      </span>
      <div className="min-w-0 flex-1">
        <p className="text-[11px] font-semibold uppercase tracking-[0.12em] text-subtle-foreground">{title}</p>
        <p className="mt-0.5 truncate text-small font-semibold text-foreground">{address.contactName}</p>
        {address.companyName === undefined ? null : <p className="truncate text-small text-muted-foreground">{address.companyName}</p>}
        <address className="mt-1 text-small not-italic leading-6 text-muted-foreground">
          <span className="block">{[address.line1, address.line2].filter(Boolean).join(', ')}</span>
          <span className="block">{[address.city, address.state, address.postalCode].filter(Boolean).join(', ')}</span>
          <span className="block">
            <span aria-hidden="true">{flagEmoji(address.countryCode)} </span>
            {countryName(address.countryCode)}
          </span>
        </address>
        <div className="mt-1.5 grid gap-0.5">
          {address.phone === undefined ? null : <ContactLine icon={Phone} value={address.phone} label="phone number" />}
          {address.email === undefined ? null : <ContactLine icon={Mail} value={address.email} label="email address" />}
        </div>
      </div>
    </div>
  );
}

export function RouteCard({ origin, destination }: { origin: ShipmentAddress; destination: ShipmentAddress }) {
  return (
    <Card className="p-5 md:p-6">
      <h2 className="font-display text-h3 font-bold">Route</h2>
      <div className="relative mt-5 grid gap-6">
        <span className="absolute bottom-8 left-[17px] top-9 border-l-2 border-dashed border-border-strong" aria-hidden="true" />
        <span className="absolute left-[6px] top-1/2 grid h-6 w-6 -translate-y-1/2 place-items-center rounded-full border border-border bg-surface text-muted-foreground shadow-xs" aria-hidden="true">
          <Truck className="h-3 w-3" />
        </span>
        <Party title="From" icon={PackageOpen} address={origin} />
        <Party title="To" icon={MapPin} address={destination} />
      </div>
    </Card>
  );
}
