'use client';

import { Check } from 'lucide-react';

import { Tooltip } from '@/components/ui/tooltip';
import { cn } from '@/lib/utils';

const STEPS = ['Addresses', 'Package', 'Options', 'Rates', 'Review'] as const;

export function Stepper({ step, maxReachable, onSelect }: { step: number; maxReachable: number; onSelect: (step: number) => void }) {
  const progress = ((step + 1) / STEPS.length) * 100;
  return (
    <nav aria-label="Shipment steps">
      {/* Mobile: Step X of Y with a thin progress bar */}
      <div className="sm:hidden">
        <div className="flex items-baseline justify-between text-small">
          <span className="font-semibold text-foreground">{STEPS[step]}</span>
          <span className="text-muted-foreground">
            Step {step + 1} of {STEPS.length}
          </span>
        </div>
        <div className="mt-2 h-1 overflow-hidden rounded-pill bg-surface-muted" aria-hidden="true">
          <div className="h-full rounded-pill bg-brand-gradient transition-[width] duration-280 ease-brand" style={{ width: `${progress}%` }} />
        </div>
      </div>

      {/* Desktop: numbered circles joined by lines */}
      <ol className="hidden items-start sm:flex">
        {STEPS.map((label, index) => {
          const complete = index < step;
          const current = index === step;
          const reachable = index <= maxReachable;
          const circle = (
            <button
              type="button"
              aria-current={current ? 'step' : undefined}
              aria-label={`${label}${complete ? ', completed' : current ? ', current step' : ''}`}
              disabled={!reachable || current}
              onClick={() => onSelect(index)}
              className={cn(
                'relative z-[1] grid h-8 w-8 place-items-center rounded-full text-small font-bold transition-[background-color,border-color,box-shadow,color] duration-280 ease-brand',
                complete && 'bg-primary text-primary-foreground hover:bg-primary-hover',
                current && 'border-2 border-primary bg-primary-soft text-primary shadow-glow',
                !complete && !current && 'border-2 border-border bg-surface text-subtle-foreground',
                reachable && !current ? 'cursor-pointer' : 'cursor-default',
                'disabled:opacity-100',
              )}
            >
              {complete ? <Check className="h-4 w-4" strokeWidth={3} aria-hidden="true" /> : index + 1}
            </button>
          );
          return (
            <li key={label} className="relative flex flex-1 flex-col items-center">
              {index < STEPS.length - 1 ? (
                <span className="absolute left-[calc(50%+20px)] right-[calc(-50%+20px)] top-[15px] h-0.5 overflow-hidden rounded-pill bg-border" aria-hidden="true">
                  <span className={cn('block h-full origin-left bg-primary transition-transform duration-500 ease-brand', complete ? 'scale-x-100' : 'scale-x-0')} />
                </span>
              ) : null}
              {complete && reachable ? (
                <Tooltip label={`Back to ${label}`} side="bottom">
                  {circle}
                </Tooltip>
              ) : (
                circle
              )}
              <span className={cn('mt-2 text-[13px]', current ? 'font-semibold text-foreground' : complete ? 'font-medium text-foreground' : 'text-subtle-foreground')}>{label}</span>
            </li>
          );
        })}
      </ol>
    </nav>
  );
}

export const WIZARD_STEPS = STEPS;
