import { Card } from '@/components/ui/card';
import { MoneyText } from '@/components/ui/money-text';
import { countryName, flagEmoji } from '@/lib/format';
import type { ShipmentListItem } from '@/lib/shipments';

const head = 'h-10 px-3 text-left text-caption font-semibold uppercase tracking-[0.08em] text-muted-foreground first:pl-5 last:pr-5 md:first:pl-6 md:last:pr-6';
const cell = 'px-3 py-3 first:pl-5 last:pr-5 md:first:pl-6 md:last:pr-6';

export function PackageItemsTable({ shipment }: { shipment: ShipmentListItem }) {
  const totalWeight = shipment.parcels.reduce((sum, parcel) => sum + parcel.weightKg, 0);
  const totalQty = shipment.items.reduce((sum, item) => sum + item.quantity, 0);
  const totalValue = shipment.items.reduce((sum, item) => sum + item.declaredValueMinor * item.quantity, 0);
  const itemCurrency = shipment.items[0]?.currency ?? shipment.currency;

  return (
    <Card className="overflow-hidden p-0">
      <div className="px-5 pb-3 pt-5 md:px-6 md:pt-6">
        <h2 className="font-display text-h3 font-bold">Package and items</h2>
      </div>
      <div className="overflow-x-auto">
        <table className="w-full min-w-[420px] text-small">
          <thead className="border-y border-border bg-surface-muted/70">
            <tr>
              <th scope="col" className={head}>Parcel</th>
              <th scope="col" className={`${head} text-right`}>Weight</th>
              <th scope="col" className={`${head} text-right`}>Dimensions</th>
            </tr>
          </thead>
          <tbody>
            {shipment.parcels.map((parcel, index) => (
              <tr key={index} className="border-b border-border">
                <td className={`${cell} font-medium`}>Parcel {index + 1}</td>
                <td className={`${cell} text-right tabular-nums`}>{parcel.weightKg} kg</td>
                <td className={`${cell} text-right tabular-nums text-muted-foreground`}>
                  {parcel.lengthCm} × {parcel.widthCm} × {parcel.heightCm} cm
                </td>
              </tr>
            ))}
          </tbody>
          {shipment.parcels.length > 1 ? (
            <tfoot>
              <tr className="border-b border-border bg-surface-muted/40 font-semibold">
                <td className={cell}>Total</td>
                <td className={`${cell} text-right tabular-nums`}>{Math.round(totalWeight * 1000) / 1000} kg</td>
                <td className={cell} />
              </tr>
            </tfoot>
          ) : null}
        </table>
      </div>
      {shipment.items.length === 0 ? (
        <p className="px-5 py-4 text-small text-muted-foreground md:px-6">No customs items were declared.</p>
      ) : (
        <div className="overflow-x-auto">
          <table className="w-full min-w-[640px] text-small">
            <thead className="border-b border-border bg-surface-muted/70">
              <tr>
                <th scope="col" className={head}>Item</th>
                <th scope="col" className={`${head} text-right`}>Qty</th>
                <th scope="col" className={`${head} text-right`}>Value</th>
                <th scope="col" className={head}>HS code</th>
                <th scope="col" className={head}>Origin</th>
              </tr>
            </thead>
            <tbody>
              {shipment.items.map((item, index) => (
                <tr key={index} className="border-b border-border">
                  <td className={`${cell} font-medium`}>
                    {item.description}
                    {item.sku === undefined ? null : <span className="block text-caption font-normal text-muted-foreground">SKU {item.sku}</span>}
                  </td>
                  <td className={`${cell} text-right tabular-nums`}>{item.quantity}</td>
                  <td className={`${cell} text-right tabular-nums`}>
                    <MoneyText minor={item.declaredValueMinor} currency={item.currency} />
                  </td>
                  <td className={`${cell} font-mono text-caption text-muted-foreground`}>{item.hsCode ?? '—'}</td>
                  <td className={`${cell} text-muted-foreground`}>
                    {item.originCountryCode === undefined ? (
                      '—'
                    ) : (
                      <>
                        <span aria-hidden="true">{flagEmoji(item.originCountryCode)} </span>
                        {countryName(item.originCountryCode)}
                      </>
                    )}
                  </td>
                </tr>
              ))}
            </tbody>
            <tfoot>
              <tr className="bg-surface-muted/40 font-semibold">
                <td className={cell}>Total declared</td>
                <td className={`${cell} text-right tabular-nums`}>{totalQty}</td>
                <td className={`${cell} text-right tabular-nums`}>
                  <MoneyText minor={totalValue} currency={itemCurrency} />
                </td>
                <td className={cell} colSpan={2} />
              </tr>
            </tfoot>
          </table>
        </div>
      )}
    </Card>
  );
}
