import { Check, Clock } from 'lucide-react';
import type { ReactNode } from 'react';

import { cn } from '@/lib/utils';

/**
 * Small code-built pieces of fake product UI used by the marketing pages.
 * Every value here is a static illustration value.
 */

const tones = {
  neutral: 'bg-surface-muted text-muted-foreground [--dot:rgb(var(--subtle-foreground))]',
  info: 'bg-info-soft text-info [--dot:rgb(var(--info))]',
  primary: 'bg-primary-soft text-primary [--dot:rgb(var(--primary))]',
  warning: 'bg-warning-soft text-warning [--dot:rgb(var(--warning))]',
  success: 'bg-success-soft text-success [--dot:rgb(var(--success))]',
  danger: 'bg-danger-soft text-danger [--dot:rgb(var(--danger))]',
  accent: 'bg-primary-soft text-accent [--dot:rgb(var(--accent))]',
} as const;

export type MockTone = keyof typeof tones;

export const mockStatusTone: Record<string, MockTone> = {
  'Label ready': 'primary',
  'In transit': 'warning',
  'Out for delivery': 'warning',
  Delivered: 'success',
  Exception: 'danger',
  Cancelled: 'neutral',
  Created: 'info',
  Returned: 'accent',
};

export function MockBadge({ tone = 'neutral', children, className }: { tone?: MockTone; children: ReactNode; className?: string }) {
  return (
    <span className={cn('inline-flex items-center gap-1.5 whitespace-nowrap rounded-pill px-2 py-0.5 text-[11px] font-semibold', tones[tone], className)}>
      <span className="h-1.5 w-1.5 rounded-full bg-[var(--dot)]" aria-hidden="true" />
      {children}
    </span>
  );
}

export function MockStatus({ status, className }: { status: string; className?: string }) {
  return (
    <MockBadge tone={mockStatusTone[status] ?? 'neutral'} className={cn(status === 'Cancelled' && 'line-through', className)}>
      {status}
    </MockBadge>
  );
}

export function MockAvatar({ initials, className }: { initials: string; className?: string }) {
  return (
    <span
      className={cn(
        'grid h-9 w-9 shrink-0 place-items-center rounded-md bg-gradient-to-br from-primary-soft to-surface-muted text-caption font-bold text-primary ring-1 ring-inset ring-border',
        className,
      )}
    >
      {initials}
    </span>
  );
}

export const mockRates = [
  { initials: 'EA', name: 'Express Air', eta: '2 to 3 days', price: '48.20', selected: false },
  { initials: 'PI', name: 'Priority International', eta: '3 to 5 days', price: '36.90', selected: true },
  { initials: 'EG', name: 'Economy Ground', eta: '6 to 9 days', price: '24.10', selected: false },
] as const;

export function MockRateRow({
  rate,
  compact = false,
  badge,
}: {
  rate: (typeof mockRates)[number];
  compact?: boolean;
  badge?: ReactNode;
}) {
  return (
    <div
      className={cn(
        'relative flex items-center gap-3 rounded-lg border transition-colors',
        compact ? 'px-3 py-2.5' : 'px-3 py-3',
        rate.selected ? 'border-2 border-primary bg-primary-soft' : 'border-border bg-surface',
      )}
    >
      <MockAvatar initials={rate.initials} className={compact ? 'h-8 w-8' : undefined} />
      <div className="min-w-0 flex-1">
        <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
          <p className="truncate text-small font-semibold text-foreground">{rate.name}</p>
          {badge}
        </div>
        <p className="mt-0.5 inline-flex items-center gap-1 text-caption text-muted-foreground">
          <Clock className="h-3 w-3" aria-hidden="true" />
          {rate.eta}
        </p>
      </div>
      <p className="font-display text-[15px] font-bold tabular-nums text-foreground">{rate.price}</p>
      <span
        className={cn(
          'grid h-[18px] w-[18px] shrink-0 place-items-center rounded-full border-2',
          rate.selected ? 'border-primary bg-primary text-primary-foreground' : 'border-border-strong bg-surface',
        )}
        aria-hidden="true"
      >
        {rate.selected ? <Check className="h-2.5 w-2.5" strokeWidth={3.5} /> : null}
      </span>
    </div>
  );
}

export function MockTimeline({
  events,
  currentIndex,
  done = false,
  className,
}: {
  events: ReadonlyArray<{ title: string; meta?: string }>;
  currentIndex: number;
  /** When true the last event is shown as completed (delivered) with a check. */
  done?: boolean;
  className?: string;
}) {
  return (
    <ol className={cn('grid', className)}>
      {events.map((event, index) => {
        const isCurrent = index === currentIndex;
        const isLast = index === events.length - 1;
        const delivered = done && isLast;
        return (
          <li key={event.title} className="relative flex gap-3 pb-4 last:pb-0">
            {isLast ? null : <span className="absolute left-[7px] top-4 h-[calc(100%-12px)] w-px bg-border-strong" aria-hidden="true" />}
            <span className="relative mt-0.5 grid h-[15px] w-[15px] shrink-0 place-items-center" aria-hidden="true">
              {isCurrent && !delivered ? <span className="absolute inset-0 animate-pulse-dot rounded-full bg-primary/40" /> : null}
              {delivered ? (
                <span className="grid h-[15px] w-[15px] place-items-center rounded-full bg-success text-primary-foreground">
                  <Check className="h-2.5 w-2.5" strokeWidth={3.5} />
                </span>
              ) : (
                <span
                  className={cn(
                    'relative h-[9px] w-[9px] rounded-full ring-[3px]',
                    isCurrent ? 'bg-primary ring-primary-soft' : 'bg-border-strong ring-surface',
                  )}
                />
              )}
            </span>
            <div className="min-w-0 leading-tight">
              <p className={cn('text-caption', isCurrent || delivered ? 'font-semibold text-foreground' : 'text-muted-foreground')}>{event.title}</p>
              {event.meta === undefined ? null : <p className="mt-0.5 text-[11px] text-subtle-foreground">{event.meta}</p>}
            </div>
          </li>
        );
      })}
    </ol>
  );
}

export function Barcode({ bars = 36, className }: { bars?: number; className?: string }) {
  return (
    <div className={cn('flex items-stretch gap-[2px]', className)} aria-hidden="true">
      {Array.from({ length: bars }).map((_, index) => (
        <span
          key={index}
          className="rounded-[1px] bg-foreground/85"
          style={{ flexGrow: 1 + ((index * 7) % 3), opacity: index % 5 === 0 ? 0.35 : 1 }}
        />
      ))}
    </div>
  );
}
