import { AlertTriangle, ChevronRight, CircleAlert, Clock3, MailWarning, Wallet, type LucideIcon } from 'lucide-react';
import Link from 'next/link';

import { DashCard } from '@/components/dashboard/dash-card';
import { SuccessBurst } from '@/components/illustrations/success-burst';
import { EmptyState, ErrorState } from '@/components/ui/states';
import { Skeleton } from '@/components/ui/skeleton';
import type { AttentionItem } from '@/lib/dashboard';
import { cn } from '@/lib/utils';

const kinds: Record<AttentionItem['type'], { icon: LucideIcon; chip: string; title: string }> = {
  exception: { icon: AlertTriangle, chip: 'bg-danger-soft text-danger', title: 'Shipment exception' },
  failed: { icon: CircleAlert, chip: 'bg-danger-soft text-danger', title: 'Label purchase failed' },
  stuck_pending: { icon: Clock3, chip: 'bg-warning-soft text-warning', title: 'Shipment stuck in pending' },
  low_balance: { icon: Wallet, chip: 'bg-warning-soft text-warning', title: 'Low wallet balance' },
  email_unverified: { icon: MailWarning, chip: 'bg-info-soft text-info', title: 'Email not verified' },
};

export function AttentionList({
  items,
  loading,
  error,
  onRetry,
}: {
  items: AttentionItem[];
  loading?: boolean;
  error?: string;
  onRetry?: () => void;
}) {
  return (
    <DashCard
      title="Attention needed"
      caption={!loading && error === undefined && items.length > 0 ? `${items.length} ${items.length === 1 ? 'item' : 'items'}` : undefined}
    >
      {loading ? (
        <div className="grid gap-4">
          {[0, 1, 2].map((index) => (
            <div key={index} className="flex items-center gap-3">
              <Skeleton className="h-9 w-9 rounded-lg" />
              <div className="grid flex-1 gap-1.5">
                <Skeleton className="h-4 w-2/3" />
                <Skeleton className="h-3 w-1/3" />
              </div>
            </div>
          ))}
        </div>
      ) : null}
      {!loading && error !== undefined ? <ErrorState message={error} onRetry={onRetry} /> : null}
      {!loading && error === undefined && items.length === 0 ? (
        <EmptyState compact illustration={<SuccessBurst size={72} />} title="Nothing needs your attention" description="Exceptions, failed labels and low balance alerts will show up here." />
      ) : null}
      {!loading && error === undefined && items.length > 0 ? (
        <ul className="-mx-2 grid gap-0.5">
          {items.map((item) => {
            const kind = kinds[item.type];
            const Icon = kind.icon;
            const href = item.type === 'email_unverified' ? '/verify-email/pending' : item.shipmentId === undefined ? '/wallet' : `/shipments/${item.shipmentId}`;
            return (
              <li key={`${item.type}-${item.shipmentId ?? 'account'}`}>
                <Link href={href} className="group flex items-center gap-3 rounded-lg px-2 py-2.5 transition-colors hover:bg-surface-muted focus-visible:bg-surface-muted">
                  <span className={cn('grid h-9 w-9 shrink-0 place-items-center rounded-lg', kind.chip)}>
                    <Icon className="h-[18px] w-[18px]" aria-hidden="true" />
                  </span>
                  <span className="min-w-0 flex-1">
                    <span className="block truncate text-small font-semibold text-foreground">{item.message}</span>
                    <span className="block truncate text-caption text-muted-foreground">{kind.title}</span>
                  </span>
                  <ChevronRight
                    className="h-4 w-4 shrink-0 text-subtle-foreground opacity-0 transition-[opacity,transform] duration-180 group-hover:translate-x-0.5 group-hover:opacity-100 group-focus-visible:opacity-100"
                    aria-hidden="true"
                  />
                </Link>
              </li>
            );
          })}
        </ul>
      ) : null}
    </DashCard>
  );
}
