'use client';

import { motion, useMotionValue, useReducedMotion, useSpring, useTransform } from 'framer-motion';
import { CheckCircle2, MapPin, Wallet } from 'lucide-react';
import { useEffect, useRef } from 'react';

import { MockBadge, MockRateRow, MockTimeline, mockRates } from '@/components/marketing/mock-ui';
import { cn } from '@/lib/utils';

const spring = { stiffness: 120, damping: 18 };

/** Code-built product composition for the home hero. All numbers are static illustration values. */
export function HeroVisual() {
  const reduce = useReducedMotion();
  const ref = useRef<HTMLDivElement>(null);
  const mx = useMotionValue(0);
  const my = useMotionValue(0);
  const rotateX = useSpring(useTransform(my, [-0.5, 0.5], [4, -4]), spring);
  const rotateY = useSpring(useTransform(mx, [-0.5, 0.5], [-4, 4]), spring);

  useEffect(() => {
    if (reduce) return undefined;
    const node = ref.current;
    if (node === null) return undefined;
    if (!window.matchMedia('(hover: hover) and (pointer: fine)').matches) return undefined;
    const onMove = (event: PointerEvent) => {
      const rect = node.getBoundingClientRect();
      mx.set(Math.max(-0.5, Math.min(0.5, (event.clientX - rect.left) / rect.width - 0.5)));
      my.set(Math.max(-0.5, Math.min(0.5, (event.clientY - rect.top) / rect.height - 0.5)));
    };
    const onLeave = () => {
      mx.set(0);
      my.set(0);
    };
    window.addEventListener('pointermove', onMove, { passive: true });
    document.documentElement.addEventListener('pointerleave', onLeave);
    return () => {
      window.removeEventListener('pointermove', onMove);
      document.documentElement.removeEventListener('pointerleave', onLeave);
    };
  }, [mx, my, reduce]);

  return (
    <motion.div
      ref={ref}
      aria-hidden="true"
      className="relative mx-auto w-full max-w-[520px] px-3 pb-14 pt-8 sm:px-6 lg:mr-0"
      initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96 }}
      animate={{ opacity: 1, scale: 1 }}
      transition={{ duration: 0.4, delay: 0.1, ease: [0.2, 0.8, 0.2, 1] }}
    >
      <motion.div style={reduce ? undefined : { rotateX, rotateY, transformPerspective: 1000 }} className="relative [transform-style:preserve-3d]">
        {/* Soft glow under the card */}
        <div className="absolute inset-x-8 -bottom-6 top-10 rounded-2xl bg-brand-gradient opacity-25 blur-3xl" />

        <div className="gradient-border relative rounded-2xl !bg-surface/85 p-4 shadow-lg backdrop-blur-[12px] sm:p-5">
          <div className="flex items-start justify-between gap-3">
            <div>
              <p className="font-display text-h3 font-bold text-foreground">Rates</p>
              <p className="mt-0.5 inline-flex items-center gap-1 text-small text-muted-foreground">
                <MapPin className="h-3.5 w-3.5" />
                Berlin to Toronto · 2.4 kg
              </p>
            </div>
            <span className="rounded-pill border border-border bg-surface-muted px-2.5 py-1 text-caption font-medium text-muted-foreground">3 services</span>
          </div>
          <div className="mt-4 grid gap-2">
            {mockRates.map((rate) => (
              <MockRateRow key={rate.name} rate={rate} badge={rate.selected ? <MockBadge tone="success">Cheapest</MockBadge> : undefined} />
            ))}
          </div>
          <div className="mt-4 flex items-center justify-between rounded-lg bg-surface-muted px-3 py-2.5">
            <span className="text-caption text-muted-foreground">Total charged to wallet</span>
            <span className="font-display text-[15px] font-bold tabular-nums">36.90</span>
          </div>
        </div>

        {/* Floating tracking card, lower left */}
        <div className="absolute -bottom-14 -left-3 w-[11.5rem] sm:-left-8">
          <div className={cn('rounded-xl border border-border bg-surface p-3.5 shadow-md', !reduce && 'animate-float')} style={{ animationDuration: '5s' }}>
            <p className="mb-2.5 text-[11px] font-semibold uppercase tracking-[0.12em] text-subtle-foreground">Tracking</p>
            <MockTimeline
              currentIndex={2}
              events={[{ title: 'Label created' }, { title: 'Picked up' }, { title: 'In transit' }]}
            />
          </div>
        </div>

        {/* Floating "Label ready" pill, top right */}
        <div className="absolute -right-2 -top-5 sm:-right-6">
          <div
            className={cn('inline-flex items-center gap-1.5 rounded-pill border border-border bg-surface py-1.5 pl-1.5 pr-3 text-caption font-semibold shadow-md', !reduce && 'animate-float')}
            style={{ animationDuration: '6s', animationDelay: '-2s' }}
          >
            <span className="grid h-6 w-6 place-items-center rounded-full bg-success-soft">
              <CheckCircle2 className="h-4 w-4 text-success" />
            </span>
            Label ready
          </div>
        </div>

        {/* Floating wallet chip, bottom right */}
        <div className="absolute -bottom-6 -right-2 sm:-right-5">
          <div
            className={cn('inline-flex items-center gap-2 rounded-pill border border-border bg-surface py-1.5 pl-1.5 pr-3 text-caption font-semibold shadow-md', !reduce && 'animate-float')}
            style={{ animationDuration: '7s', animationDelay: '-4s' }}
          >
            <span className="grid h-6 w-6 place-items-center rounded-full bg-primary-soft">
              <Wallet className="h-3.5 w-3.5 text-primary" />
            </span>
            <span className="text-muted-foreground">Balance</span>
            <span className="tabular-nums">250.00</span>
          </div>
        </div>
      </motion.div>
    </motion.div>
  );
}
