import { Download, History, MapPin, Scale, Tags, TrendingUp, Users, Wallet } from 'lucide-react';
import type { LucideIcon } from 'lucide-react';

import { Container, Section, SectionHeading } from '@/components/layout/container';
import { Barcode, MockStatus } from '@/components/marketing/mock-ui';
import { Reveal } from '@/components/motion/reveal';
import { Card } from '@/components/ui/card';
import { cn } from '@/lib/utils';

type Visual = 'bars' | 'barcode' | 'route' | 'wallet' | 'team' | 'table';

// Bento layout: 3 columns x 3 rows on desktop, 2 columns on tablet, 1 on mobile.
// Row 1: Compare (2 cols) + Labels (spans 2 rows). Row 2: Tracking, Wallet. Row 3: Team + History (2 cols).
const cards: ReadonlyArray<{ title: string; text: string; visual: Visual; icon: LucideIcon; span: string }> = [
  { title: 'Compare rates', text: 'See every available service side by side before you spend.', visual: 'bars', icon: Scale, span: 'md:col-span-2' },
  { title: 'Labels in one click', text: 'Buy the label from the rate you already compared.', visual: 'barcode', icon: Tags, span: 'lg:row-span-2' },
  { title: 'Live tracking', text: 'Follow every parcel from pickup to doorstep.', visual: 'route', icon: MapPin, span: '' },
  { title: 'Prepaid wallet', text: 'Top up once and ship as you go with a clear ledger.', visual: 'wallet', icon: Wallet, span: '' },
  { title: 'Team access', text: 'Invite your team with clear roles and permissions.', visual: 'team', icon: Users, span: '' },
  { title: 'Complete history', text: 'Filter, search and export every shipment you have created.', visual: 'table', icon: History, span: 'md:col-span-2' },
];

function MiniVisual({ kind }: { kind: Visual }) {
  if (kind === 'bars') {
    const bars = [
      { label: 'Express Air', width: 92, price: '48.20' },
      { label: 'Economy Ground', width: 46, price: '24.10', best: true },
      { label: 'Priority International', width: 70, price: '36.90' },
    ];
    return (
      <div className="grid gap-3">
        {bars.map((bar) => (
          <div key={bar.label} className="grid grid-cols-[minmax(0,1fr)_auto] items-center gap-3">
            <div className="h-7 overflow-hidden rounded-md bg-surface-muted">
              <div
                className={cn(
                  'flex h-full items-center rounded-md px-2.5 text-[11px] font-semibold',
                  bar.best ? 'bg-brand-gradient text-primary-foreground' : 'bg-border text-muted-foreground',
                )}
                style={{ width: `${bar.width}%` }}
              >
                <span className="truncate">{bar.label}</span>
              </div>
            </div>
            <span className={cn('font-display text-small font-bold tabular-nums', bar.best ? 'text-primary' : 'text-muted-foreground')}>{bar.price}</span>
          </div>
        ))}
      </div>
    );
  }
  if (kind === 'barcode') {
    return (
      <div className="flex flex-1 flex-col rounded-lg border border-dashed border-border-strong bg-surface p-4">
        <div className="flex items-center justify-between text-[11px] font-semibold uppercase tracking-[0.12em] text-subtle-foreground">
          <span>Priority Intl.</span>
          <span>2.4 kg</span>
        </div>
        <div className="mt-3 grid gap-1 text-caption">
          <p className="font-semibold text-foreground">To: Toronto, CA</p>
          <p className="text-muted-foreground">From: Berlin, DE</p>
        </div>
        <Barcode className="mt-4 h-14 lg:h-20" />
        <p className="mt-2 text-center font-mono text-[11px] tracking-[0.2em] text-muted-foreground">SHP 1042 7781 DE</p>
        <span className="mt-4 inline-flex items-center justify-center gap-1.5 rounded-md bg-brand-gradient px-3 py-2 text-caption font-semibold text-primary-foreground shadow-sm lg:mt-auto">
          <Download className="h-3.5 w-3.5" aria-hidden="true" />
          Download label
        </span>
      </div>
    );
  }
  if (kind === 'route') {
    return (
      <div className="pt-2">
        <div className="flex items-center">
          {['Berlin', 'Frankfurt', 'Toronto'].map((city, index) => (
            <div key={city} className={cn('flex items-center', index < 2 && 'flex-1')}>
              <span className="relative grid h-4 w-4 place-items-center">
                {index === 2 ? <span className="absolute inset-0 animate-pulse-dot rounded-full bg-primary/40" /> : null}
                <span className={cn('relative h-2.5 w-2.5 rounded-full ring-4', index === 2 ? 'bg-primary ring-primary-soft' : 'bg-border-strong ring-surface-muted')} />
              </span>
              {index < 2 ? <span className="mx-1 h-0 flex-1 border-t-2 border-dashed border-border-strong" /> : null}
            </div>
          ))}
        </div>
        <div className="mt-2 flex justify-between text-[11px] text-muted-foreground">
          <span>Berlin</span>
          <span>Frankfurt</span>
          <span className="font-semibold text-foreground">Toronto</span>
        </div>
      </div>
    );
  }
  if (kind === 'wallet') {
    return (
      <div className="flex items-end justify-between gap-3">
        <div>
          <p className="text-[11px] font-medium text-muted-foreground">Balance</p>
          <p className="font-display text-h2 font-bold tabular-nums">250.00</p>
          <span className="mt-1 inline-flex items-center gap-1 rounded-pill bg-success-soft px-2 py-0.5 text-[11px] font-semibold text-success">
            <TrendingUp className="h-3 w-3" aria-hidden="true" />
            +100.00
          </span>
        </div>
        <svg viewBox="0 0 96 40" className="h-12 w-28 text-primary" aria-hidden="true">
          <defs>
            <linearGradient id="spark-fill" x1="0" x2="0" y1="0" y2="1">
              <stop offset="0%" stopColor="currentColor" stopOpacity="0.25" />
              <stop offset="100%" stopColor="currentColor" stopOpacity="0" />
            </linearGradient>
          </defs>
          <path d="M2 34 L18 30 L32 32 L46 22 L60 24 L74 12 L94 6 L94 40 L2 40 Z" fill="url(#spark-fill)" />
          <path d="M2 34 L18 30 L32 32 L46 22 L60 24 L74 12 L94 6" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
          <circle cx="94" cy="6" r="3" fill="currentColor" />
        </svg>
      </div>
    );
  }
  if (kind === 'team') {
    return (
      <div className="grid gap-3">
        <div className="flex -space-x-2">
          {[
            ['AC', 'from-primary to-accent'],
            ['NB', 'from-info to-primary'],
            ['MO', 'from-accent to-danger'],
            ['LP', 'from-success to-info'],
          ].map(([initials, gradient]) => (
            <span key={initials} className={cn('grid h-9 w-9 place-items-center rounded-full border-2 border-surface bg-gradient-to-br text-[11px] font-bold text-primary-foreground', gradient)}>
              {initials}
            </span>
          ))}
        </div>
        <div className="flex flex-wrap gap-1.5">
          <span className="rounded-pill bg-primary-soft px-2 py-0.5 text-[11px] font-semibold text-primary">Owner</span>
          <span className="rounded-pill bg-info-soft px-2 py-0.5 text-[11px] font-semibold text-info">Admin</span>
          <span className="rounded-pill bg-surface-muted px-2 py-0.5 text-[11px] font-semibold text-muted-foreground">Member</span>
        </div>
      </div>
    );
  }
  return (
    <div className="overflow-hidden rounded-lg border border-border">
      {[
        { id: 'SH-1042', city: 'Toronto', status: 'Label ready' },
        { id: 'SH-1041', city: 'Oslo', status: 'In transit' },
        { id: 'SH-1040', city: 'Lyon', status: 'Delivered' },
      ].map((row, index) => (
        <div key={row.id} className={cn('flex items-center gap-3 px-3 py-2.5 text-caption', index > 0 && 'border-t border-border', index === 2 && 'opacity-70')}>
          <span className="font-mono font-semibold text-foreground">{row.id}</span>
          <span className="flex-1 truncate text-muted-foreground">{row.city}</span>
          <MockStatus status={row.status} />
        </div>
      ))}
    </div>
  );
}

export function FeaturesBento() {
  return (
    <Section id="features">
      <Container>
        <Reveal>
          <SectionHeading eyebrow="Everything you need" title="One platform for every shipment" text="Rates, labels, tracking and billing without juggling five tools." />
        </Reveal>
        <div className="mt-12 grid gap-4 md:grid-cols-2 lg:grid-cols-3">
          {cards.map((card, index) => {
            const Icon = card.icon;
            return (
              <Reveal key={card.title} index={index} className={card.span}>
                <Card variant="interactive" className="group flex h-full flex-col p-6 hover:shadow-[0_8px_24px_-6px_rgb(var(--primary)/0.18),0_0_0_4px_rgb(var(--primary)/0.06)]">
                  <div className="grid h-11 w-11 place-items-center rounded-xl bg-primary-soft text-primary">
                    <Icon className="h-5 w-5" aria-hidden="true" />
                  </div>
                  <h3 className="mt-4 font-display text-h3 font-bold">{card.title}</h3>
                  <p className="mt-1.5 text-body text-muted-foreground">{card.text}</p>
                  <div className="mt-6 flex flex-1 flex-col justify-end transition-transform duration-280 ease-brand group-hover:scale-[1.03]" aria-hidden="true">
                    <MiniVisual kind={card.visual} />
                  </div>
                </Card>
              </Reveal>
            );
          })}
        </div>
      </Container>
    </Section>
  );
}
