'use client';

import { HelpCircle, MapPin, Search, Timer } from 'lucide-react';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import { useState, type FormEvent } from 'react';

import { RouteIllustration } from '@/components/illustrations/route-illustration';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { site } from '@/lib/marketing';
import { cn } from '@/lib/utils';

const tips = [
  {
    title: 'Where to find your number',
    text: 'It is printed on the shipping label and shown on the shipment once the label is ready.',
    icon: MapPin,
  },
  {
    title: 'How long updates take',
    text: 'Couriers usually post the first scan within a few hours of pickup.',
    icon: Timer,
  },
  {
    title: 'Need help',
    text: `Contact support with the tracking number at ${site.supportEmail}.`,
    icon: HelpCircle,
  },
] as const;

export function TrackLookup() {
  const router = useRouter();
  const [trackingNumber, setTrackingNumber] = useState('');
  const [error, setError] = useState<string | undefined>(undefined);

  const onSubmit = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    const value = trackingNumber.trim();
    if (value.length === 0) {
      setError('Enter a tracking number.');
      return;
    }
    router.push(`/track/${encodeURIComponent(value)}`);
  };

  return (
    <div className="mx-auto grid max-w-3xl justify-items-center text-center">
      <div className="grid h-28 w-28 place-items-center rounded-2xl border border-border bg-surface/80 shadow-md backdrop-blur">
        <RouteIllustration size={88} />
      </div>
      <h1 className="mt-8 font-display text-[2.25rem] font-extrabold leading-[2.75rem] tracking-[-0.03em] md:text-display-lg">Track your parcel</h1>
      <p className="mt-4 max-w-lg text-[1.0625rem] leading-7 text-muted-foreground">Enter the tracking number from your label.</p>

      <form className="mt-8 w-full max-w-[640px]" onSubmit={onSubmit} noValidate>
        <label htmlFor="trackingNumber" className="sr-only">Tracking number</label>
        <div
          className={cn(
            'flex h-[60px] items-center gap-2 rounded-pill border border-border bg-surface pl-2 pr-1.5 shadow-md transition-[border-color,box-shadow] duration-180 focus-within:border-primary focus-within:shadow-glow',
            error !== undefined && 'border-danger focus-within:border-danger',
          )}
        >
          <Search className="ml-3 h-5 w-5 shrink-0 text-muted-foreground" aria-hidden="true" />
          <Input
            id="trackingNumber"
            value={trackingNumber}
            placeholder="e.g. SHP1042XYZ"
            aria-invalid={error !== undefined}
            aria-describedby={error === undefined ? 'track-note' : 'trackingNumber-error track-note'}
            className="h-11 min-w-0 flex-1 border-0 bg-transparent px-2 font-mono tracking-wide shadow-none placeholder:font-sans placeholder:tracking-normal focus-visible:shadow-none"
            onChange={(event) => {
              setTrackingNumber(event.target.value);
              if (error !== undefined) setError(undefined);
            }}
          />
          <Button type="submit" size="lg" className="shrink-0 rounded-pill px-6">Track</Button>
        </div>
        {error === undefined ? null : (
          <p id="trackingNumber-error" role="alert" className="mt-2 text-left text-small text-danger">{error}</p>
        )}
      </form>

      <div className="mt-12 grid w-full gap-3 text-left md:grid-cols-3">
        {tips.map((tip) => {
          const Icon = tip.icon;
          return (
            <article key={tip.title} className="rounded-lg border border-border bg-surface p-5 shadow-xs">
              <span className="grid h-9 w-9 place-items-center rounded-lg bg-primary-soft text-primary">
                <Icon className="h-4 w-4" aria-hidden="true" />
              </span>
              <h2 className="mt-3 text-small font-semibold">{tip.title}</h2>
              <p className="mt-1 text-small text-muted-foreground">{tip.text}</p>
            </article>
          );
        })}
      </div>

      <p id="track-note" className="mt-6 text-caption text-muted-foreground">
        Tracking works for any shipment created on {site.name}. Names, phone numbers and street addresses are not shown.{' '}
        <Link href="/contact" className="text-primary hover:underline">Need help?</Link>
      </p>
    </div>
  );
}
