'use client';

import { motion, useReducedMotion } from 'framer-motion';
import { Plus } from 'lucide-react';

import { Container, Section, SectionHeading } from '@/components/layout/container';
import { MockRateRow, MockTimeline, mockRates } from '@/components/marketing/mock-ui';
import { Reveal } from '@/components/motion/reveal';
import { Card } from '@/components/ui/card';

const steps = [
  { title: 'Add your addresses and top up', text: 'Save senders and recipients once, then fund your prepaid wallet.', mock: 'address' },
  { title: 'Compare couriers and buy a label', text: 'Pick a quoted service. Your wallet is charged the price you saw.', mock: 'rates' },
  { title: 'Track every parcel until delivery', text: 'Follow every status update and share a privacy-safe tracking page.', mock: 'track' },
] as const;

function StepMock({ kind }: { kind: (typeof steps)[number]['mock'] }) {
  if (kind === 'address') {
    return (
      <div className="grid gap-2">
        {[
          ['Sender', 'Berlin, DE'],
          ['Recipient', 'Toronto, CA'],
        ].map(([label, value]) => (
          <div key={label} className="rounded-md border border-border bg-surface px-2.5 py-1.5">
            <p className="text-[10px] font-semibold uppercase tracking-[0.12em] text-subtle-foreground">{label}</p>
            <p className="text-caption font-medium text-foreground">{value}</p>
          </div>
        ))}
        <div className="mt-1 flex items-center justify-between">
          <span className="text-caption text-muted-foreground">
            Wallet <span className="font-semibold tabular-nums text-foreground">250.00</span>
          </span>
          <span className="inline-flex items-center gap-1 rounded-md bg-brand-gradient px-2.5 py-1 text-caption font-semibold text-primary-foreground shadow-sm">
            <Plus className="h-3 w-3" aria-hidden="true" />
            Top up
          </span>
        </div>
      </div>
    );
  }
  if (kind === 'rates') {
    return (
      <div className="grid gap-2">
        {mockRates.slice(1).map((rate) => (
          <MockRateRow key={rate.name} rate={rate} compact />
        ))}
      </div>
    );
  }
  return (
    <MockTimeline
      done
      currentIndex={2}
      events={[
        { title: 'Picked up', meta: 'Berlin · Mon' },
        { title: 'In transit', meta: 'Frankfurt · Tue' },
        { title: 'Delivered', meta: 'Toronto · Thu' },
      ]}
    />
  );
}

export function HowItWorks() {
  const reduce = useReducedMotion();
  return (
    <Section muted id="how-it-works" className="scroll-mt-20">
      <Container>
        <Reveal>
          <SectionHeading
            eyebrow="Simple by design"
            title="From address to delivered in three steps"
            text="No carrier accounts to connect and no contracts to sign. Just ship."
          />
        </Reveal>
        <div className="relative mt-12 grid gap-4 md:grid-cols-3 md:gap-6">
          {/* Dashed connector behind the cards, drawn in as it enters the viewport */}
          <motion.div
            className="pointer-events-none absolute left-[16%] right-[16%] top-[3.25rem] hidden origin-left border-t-2 border-dashed border-border-strong md:block"
            aria-hidden="true"
            initial={reduce ? { opacity: 0 } : { scaleX: 0 }}
            whileInView={reduce ? { opacity: 1 } : { scaleX: 1 }}
            viewport={{ once: true, margin: '-80px' }}
            transition={{ duration: 1.1, delay: 0.2, ease: [0.2, 0.8, 0.2, 1] }}
          />
          {steps.map((step, index) => (
            <Reveal key={step.title} index={index} className="relative">
              <Card className="flex h-full flex-col p-6">
                <p
                  className="font-display text-[72px] font-extrabold leading-none tracking-[-0.04em] text-transparent [-webkit-text-stroke:1.5px_rgb(var(--primary)/0.45)]"
                  aria-hidden="true"
                >
                  {String(index + 1).padStart(2, '0')}
                </p>
                <h3 className="mt-3 font-display text-h3 font-bold">
                  <span className="sr-only">Step {index + 1}: </span>
                  {step.title}
                </h3>
                <p className="mt-1.5 text-body text-muted-foreground">{step.text}</p>
                <div className="mt-6 flex-1 rounded-lg border border-border bg-surface-muted p-3" aria-hidden="true">
                  <StepMock kind={step.mock} />
                </div>
              </Card>
            </Reveal>
          ))}
        </div>
      </Container>
    </Section>
  );
}
