// @design-system: layout/Breadcrumb
// Registered at /design-system#breadcrumb-layout

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

export interface BreadcrumbItem {
  /** Display label for this crumb. */
  label: string;
  /** If provided and not the last item, renders as a link. */
  href?: string;
  /** Mark as DB-sourced user content so i18n checks skip it. */
  userContent?: boolean;
}

export interface BreadcrumbProps {
  /** Ordered list of crumbs - home first, current page last. */
  items: BreadcrumbItem[];
  /** aria-label for the nav landmark. */
  ariaLabel?: string;
  className?: string;
  /**
   * `default` — white surface bar with bottom border (standard page use).
   * `hero-overlay` — frosted-glass pill floating over a hero image.
   */
  variant?: 'default' | 'hero-overlay';
}

/**
 * Breadcrumb - site-path navigation trail.
 *
 * - Renders nothing when fewer than 2 items (no trail to show).
 * - RTL-aware: uses `/` separator (direction-neutral).
 * - Last item is `aria-current="page"` and not linked.
 * - WCAG 2.4.8 - location within a set of pages.
 *
 * @example
 * ```tsx
 * <Breadcrumb
 *   items={[
 *     { label: 'Multideal', href: '/' },
 *     { label: t('my_purchases'), href: '/purchases' },
 *     { label: dealTitle },
 *   ]}
 * />
 * ```
 */
export function Breadcrumb({
  items,
  ariaLabel = 'breadcrumb',
  className,
  variant = 'default',
}: BreadcrumbProps) {
  if (items.length < 2) return null;

  const isOverlay = variant === 'hero-overlay';

  return (
    <nav
      aria-label={ariaLabel}
      className={cn(
        isOverlay
          ? 'inline-flex rounded-full bg-black/40 px-3 py-1.5 backdrop-blur-sm'
          : 'bg-surface-base border-b border-neutral-100 py-2',
        className,
      )}
    >
      <ol
        className={cn(
          'flex flex-wrap items-center gap-x-1',
          !isOverlay && 'mx-auto max-w-7xl px-6',
        )}
      >
        {items.map((item, index) => {
          const isLast = index === items.length - 1;
          return (
            <li key={item.href ?? `${item.label}-${index}`} className="flex items-center gap-x-1">
              {index > 0 && (
                <span
                  className={cn(
                    'text-sm select-none',
                    isOverlay ? 'text-white/60' : 'text-text-tertiary',
                  )}
                  aria-hidden="true"
                >
                  /
                </span>
              )}
              {!isLast && item.href ? (
                <a
                  href={item.href}
                  className={cn(
                    'rounded text-sm transition-colors focus-visible:outline-2 focus-visible:outline-offset-1',
                    isOverlay
                      ? 'text-white/80 hover:text-white focus-visible:outline-white'
                      : 'text-text-secondary hover:text-text-primary',
                  )}
                  {...(item.userContent ? { 'data-user-content': true } : {})}
                >
                  {item.label}
                </a>
              ) : (
                <span
                  className={cn(
                    'text-sm',
                    isLast
                      ? cn(
                          'max-w-[24ch] truncate font-medium',
                          isOverlay ? 'text-white' : 'text-text-primary',
                        )
                      : isOverlay
                        ? 'text-white/80'
                        : 'text-text-secondary',
                  )}
                  aria-current={isLast ? 'page' : undefined}
                  {...(item.userContent ? { 'data-user-content': true } : {})}
                >
                  {item.label}
                </span>
              )}
            </li>
          );
        })}
      </ol>
    </nav>
  );
}
