import { Head, Link, usePage } from '@inertiajs/react';
import {
    ArrowRight,
    CalendarDays,
    Car,
    CheckCircle2,
    Clock,
    MapPin,
    ShieldCheck,
    Sparkles,
    Star,
} from 'lucide-react';
import { PublicHeader } from '@/components/public-header';
import {
    FaqSection,
    type WelcomeFaq,
} from '@/components/homepage/faq-section';
import { Badge } from '@/components/ui/badge';
import { buttonVariants } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { cn } from '@/lib/utils';
import { dashboard, login } from '@/routes';

type WelcomePackage = {
    id: number;
    name: string;
    description: string | null;
    duration_minutes: number;
    min_price_cents: number;
    is_featured: boolean;
    icon: string | null;
    icon_svg: string | null;
    col_span: number;
    row_span: number;
};

// Static class maps so Tailwind keeps these utilities in the build.
const BENTO_COL_SPAN: Record<number, string> = {
    1: 'lg:col-span-1',
    2: 'lg:col-span-2',
    3: 'lg:col-span-3',
    4: 'sm:col-span-2 lg:col-span-4',
};

const BENTO_ROW_SPAN: Record<number, string> = {
    1: 'lg:row-span-1',
    2: 'lg:row-span-2',
    3: 'lg:row-span-3',
    4: 'lg:row-span-4',
};

function formatStartingRate(cents: number): string {
    if (cents <= 0) return 'Contact us';
    return `$${Math.floor(cents / 100)}`;
}

// The marquee row's width (and thus how far a 50% translate travels) grows
// with the package count, so the duration must scale with it too — otherwise
// more packages would visually scroll faster for the same fixed duration.
const MARQUEE_SECONDS_PER_PACKAGE = 8;

const STEPS = [
    {
        icon: MapPin,
        label: 'Choose your service',
        description: 'Pick mobile or drop-off, then select a package.',
    },
    {
        icon: CalendarDays,
        label: 'Pick a date & time',
        description: 'Choose any available slot that works for you.',
    },
    {
        icon: Car,
        label: 'We handle the rest',
        description: 'Our team arrives and delivers a spotless result.',
    },
] as const;

const TRUST = [
    { icon: Star, text: '5-star rated service' },
    { icon: ShieldCheck, text: 'Licensed & insured' },
    { icon: CheckCircle2, text: 'Satisfaction guaranteed' },
    { icon: Clock, text: 'On-time, every time' },
] as const;

type Props = {
    packages: WelcomePackage[];
    servicesStyle: 'bento' | 'list' | 'marquee';
    servicesSectionEnabled: boolean;
    faq: WelcomeFaq;
};

export default function Welcome({ packages, servicesStyle, servicesSectionEnabled, faq }: Props) {
    const { auth } = usePage().props as {
        auth?: { user?: { name?: string; role?: string } };
    };
    const isStaff = ['admin', 'manager', 'technician'].includes(
        auth?.user?.role ?? '',
    );

    return (
        <>
            <Head title="AIO Car Wash — Professional Car Wash Service" />

            <div className="min-h-screen bg-background text-foreground">
                {/* ── Header ── */}
                <header className="sticky top-0 z-20 border-b bg-background/95 backdrop-blur supports-backdrop-filter:bg-background/80">
                    <div className="mx-auto max-w-5xl px-6 py-4">
                        <PublicHeader />
                    </div>
                </header>

                {/* ── Hero ── */}
                <section className="relative overflow-hidden bg-muted px-6 py-20 text-center md:py-32 dark:bg-[#04040e]">
                    <div className="hero-dark-glow" aria-hidden="true" />
                    <div className="hero-grid" aria-hidden="true" />
                    <div className="hero-vignette" aria-hidden="true" />

                    {/* Floating decorative sparkles */}
                    <Sparkles
                        aria-hidden="true"
                        className="hero-sparkle-icon text-blue-400/40"
                        style={{
                            top: '18%',
                            left: '7%',
                            width: 28,
                            height: 28,
                            animationDelay: '0s',
                        }}
                    />
                    <Sparkles
                        aria-hidden="true"
                        className="hero-sparkle-icon text-indigo-400/30"
                        style={{
                            top: '22%',
                            right: '9%',
                            width: 20,
                            height: 20,
                            animationDelay: '1.8s',
                        }}
                    />
                    <Star
                        aria-hidden="true"
                        className="hero-sparkle-icon text-indigo-300/25"
                        style={{
                            bottom: '22%',
                            left: '11%',
                            width: 16,
                            height: 16,
                            animationDelay: '0.9s',
                        }}
                    />
                    <Sparkles
                        aria-hidden="true"
                        className="hero-sparkle-icon text-blue-300/20"
                        style={{
                            bottom: '28%',
                            right: '13%',
                            width: 14,
                            height: 14,
                            animationDelay: '2.4s',
                        }}
                    />

                    <div className="relative z-10 mx-auto max-w-2xl">
                        <Badge className="mb-6 border border-primary/25 bg-primary/10 px-4 py-3 text-sm text-primary dark:border-indigo-600/35 dark:bg-indigo-600/20 dark:text-indigo-100">
                            <Sparkles className="mr-1.5 inline size-3 text-primary dark:text-indigo-400" />
                            Professional · Affordable · Reliable
                        </Badge>

                        <h1 className="text-4xl leading-tight font-extrabold tracking-tight text-foreground md:text-6xl dark:text-white">
                            Your car,{' '}
                            <span className="shimmer-text">spotless.</span>
                        </h1>

                        <p className="mx-auto mt-5 max-w-lg text-xl leading-relaxed text-muted-foreground dark:text-white/60">
                            Book a professional car wash in minutes — mobile or
                            at our location. No hassle, just a clean car.
                        </p>

                        {/* Social proof */}
                        <div className="mt-6 flex items-center justify-center gap-2.5">
                            <div className="flex">
                                {Array.from({ length: 5 }).map((_, i) => (
                                    <Star
                                        key={i}
                                        className="size-4 fill-amber-400 text-amber-400"
                                    />
                                ))}
                            </div>
                            <span className="text-sm text-muted-foreground dark:text-white/50">
                                <span className="font-semibold text-foreground dark:text-white/80">
                                    500+
                                </span>{' '}
                                happy customers
                            </span>
                        </div>

                        <div className="mt-8 flex flex-col items-center gap-3">
                            <Link
                                href="/book/location"
                                className={cn(
                                    buttonVariants({ size: 'lg' }),
                                    'h-16 w-full max-w-sm gap-2 text-lg font-semibold shadow-lg shadow-indigo-500/20',
                                )}
                            >
                                Book a Car Wash Now
                                <ArrowRight className="size-5" />
                            </Link>
                            <p className="text-sm text-muted-foreground dark:text-white/40">
                                No account needed &nbsp;·&nbsp; Takes about 2
                                minutes
                            </p>
                        </div>
                    </div>
                </section>

                <Separator />

                {/* ── How it works ── */}
                <section className="px-6 py-16">
                    <div className="mx-auto max-w-4xl">
                        <h2 className="mb-2 text-center text-3xl font-bold">
                            How it works
                        </h2>
                        <p className="mb-12 text-center text-muted-foreground">
                            Three simple steps and your car is on its way to
                            sparkling clean.
                        </p>

                        <div className="grid gap-8 md:grid-cols-3">
                            {STEPS.map(
                                ({ icon: Icon, label, description }, i) => (
                                    <div
                                        key={label}
                                        className="flex flex-col items-center text-center"
                                    >
                                        <div className="relative mb-4">
                                            <div className="flex size-16 items-center justify-center rounded-2xl bg-primary/10">
                                                <Icon className="size-8 text-primary" />
                                            </div>
                                            <span className="absolute -top-2 -right-2 flex size-6 items-center justify-center rounded-full bg-primary text-xs font-bold text-primary-foreground">
                                                {i + 1}
                                            </span>
                                        </div>
                                        <h3 className="mb-2 text-lg font-semibold">
                                            {label}
                                        </h3>
                                        <p className="text-sm leading-relaxed text-muted-foreground">
                                            {description}
                                        </p>
                                    </div>
                                ),
                            )}
                        </div>
                    </div>
                </section>

                {servicesSectionEnabled && (
                <>
                <Separator />

                {/* ── Services ── */}
                <section className="px-6 py-16">
                    <div className="mx-auto max-w-4xl">
                        <h2 className="mb-2 text-center text-3xl font-bold">
                            Our services
                        </h2>
                        <p className="mb-12 text-center text-muted-foreground">
                            Every service includes our quality guarantee.
                        </p>

                        {servicesStyle === 'bento' && (
                            <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-flow-dense lg:auto-rows-[minmax(12rem,1fr)] lg:grid-cols-4">
                                {packages.map((pkg) => {
                                    const isLarge = pkg.row_span >= 2;

                                    return (
                                        <div
                                            key={pkg.id}
                                            className={cn(
                                                'relative flex h-full flex-col rounded-2xl border p-6 transition-shadow hover:shadow-md',
                                                BENTO_COL_SPAN[pkg.col_span] ??
                                                    BENTO_COL_SPAN[1],
                                                BENTO_ROW_SPAN[pkg.row_span] ??
                                                    BENTO_ROW_SPAN[1],
                                                pkg.is_featured
                                                    ? 'border-primary bg-primary/5 shadow-sm ring-2 ring-primary/20'
                                                    : 'border-border bg-muted/20',
                                            )}
                                        >
                                            {pkg.is_featured && (
                                                <Badge className="absolute -top-3 left-6 px-3 py-0.5 text-xs">
                                                    Most popular
                                                </Badge>
                                            )}

                                            <div
                                                className={cn(
                                                    'pkg-icon mb-4 flex items-center justify-center rounded-xl bg-blue-50 dark:bg-slate-900',
                                                    isLarge
                                                        ? 'size-16'
                                                        : 'size-14',
                                                )}
                                            >
                                                {pkg.icon_svg ? (
                                                    <div
                                                        className={cn(
                                                            '[&_svg]:size-full',
                                                            isLarge
                                                                ? 'size-12'
                                                                : 'size-10',
                                                        )}
                                                        dangerouslySetInnerHTML={{
                                                            __html: pkg.icon_svg,
                                                        }}
                                                    />
                                                ) : (
                                                    <Sparkles
                                                        className={cn(
                                                            isLarge
                                                                ? 'size-7'
                                                                : 'size-6',
                                                            pkg.is_featured
                                                                ? 'text-primary'
                                                                : 'text-muted-foreground',
                                                        )}
                                                    />
                                                )}
                                            </div>

                                            <h3
                                                className={cn(
                                                    'font-bold',
                                                    isLarge
                                                        ? 'text-2xl'
                                                        : 'text-xl',
                                                )}
                                            >
                                                {pkg.name}
                                            </h3>
                                            <p className="mt-2 flex-1 text-sm leading-relaxed text-muted-foreground">
                                                {pkg.description}
                                            </p>

                                            <div className="mt-4 flex items-center gap-1.5 text-xs text-muted-foreground">
                                                <Clock className="size-3.5" />
                                                {pkg.duration_minutes} min
                                            </div>

                                            <div className="mt-4 flex items-end justify-between">
                                                <div>
                                                    <span
                                                        className={cn(
                                                            'block leading-none font-extrabold',
                                                            isLarge
                                                                ? 'text-3xl'
                                                                : 'text-2xl',
                                                            pkg.is_featured &&
                                                                'text-primary',
                                                        )}
                                                    >
                                                        {formatStartingRate(
                                                            pkg.min_price_cents,
                                                        )}
                                                    </span>
                                                    {pkg.min_price_cents >
                                                        0 && (
                                                        <span className="mt-1 block text-[10px] tracking-wide text-muted-foreground uppercase">
                                                            Starting rate
                                                        </span>
                                                    )}
                                                </div>
                                                <Link
                                                    href="/book/location"
                                                    className={cn(
                                                        buttonVariants({
                                                            size: 'sm',
                                                            variant:
                                                                pkg.is_featured
                                                                    ? 'default'
                                                                    : 'outline',
                                                        }),
                                                    )}
                                                >
                                                    Book now
                                                </Link>
                                            </div>
                                        </div>
                                    );
                                })}
                            </div>
                        )}

                        {servicesStyle === 'list' && (
                            <div className="mx-auto max-w-3xl overflow-hidden rounded-2xl border border-border bg-muted/20">
                                {packages.map((pkg, index) => (
                                    <div
                                        key={pkg.id}
                                        className={cn(
                                            'flex flex-col gap-4 p-5 sm:flex-row sm:items-center',
                                            index > 0 &&
                                                'border-t border-border',
                                            pkg.is_featured && 'bg-primary/5',
                                        )}
                                    >
                                        <div
                                            className={cn(
                                                'pkg-icon flex size-11 shrink-0 items-center justify-center rounded-xl',
                                                pkg.is_featured
                                                    ? 'pkg-icon-on-primary bg-primary text-primary-foreground'
                                                    : 'bg-blue-50 dark:bg-slate-900',
                                            )}
                                        >
                                            {pkg.icon_svg ? (
                                                <div
                                                    className="size-6 [&_svg]:size-full"
                                                    dangerouslySetInnerHTML={{
                                                        __html: pkg.icon_svg,
                                                    }}
                                                />
                                            ) : (
                                                <Sparkles
                                                    className={cn(
                                                        'size-5',
                                                        pkg.is_featured
                                                            ? 'text-primary-foreground'
                                                            : 'text-muted-foreground',
                                                    )}
                                                />
                                            )}
                                        </div>

                                        <div className="min-w-0 flex-1">
                                            <div className="flex flex-wrap items-center gap-2">
                                                <h3 className="font-semibold">
                                                    {pkg.name}
                                                </h3>
                                                {pkg.is_featured && (
                                                    <Badge className="px-2 py-0.5 text-[10px] tracking-wide uppercase">
                                                        Most popular
                                                    </Badge>
                                                )}
                                            </div>
                                            <p className="mt-1 flex items-center gap-1.5 text-sm text-muted-foreground">
                                                <Clock className="size-3.5 shrink-0" />
                                                <span className="font-medium text-foreground">
                                                    {pkg.duration_minutes} min
                                                </span>
                                                {pkg.description && (
                                                    <>
                                                        <span aria-hidden="true">
                                                            ·
                                                        </span>
                                                        <span className="line-clamp-2">
                                                            {pkg.description}
                                                        </span>
                                                    </>
                                                )}
                                            </p>
                                        </div>

                                        <div className="flex items-center justify-between gap-4 sm:justify-end">
                                            <div className="text-right">
                                                <div
                                                    className={cn(
                                                        'text-2xl leading-none font-extrabold',
                                                        pkg.is_featured &&
                                                            'text-primary',
                                                    )}
                                                >
                                                    {formatStartingRate(
                                                        pkg.min_price_cents,
                                                    )}
                                                </div>
                                                {pkg.min_price_cents > 0 && (
                                                    <div className="mt-1 text-[10px] tracking-wide text-muted-foreground uppercase">
                                                        Starting rate
                                                    </div>
                                                )}
                                            </div>
                                            <Link
                                                href="/book/location"
                                                className={cn(
                                                    buttonVariants({
                                                        size: 'sm',
                                                    }),
                                                    'shrink-0',
                                                )}
                                            >
                                                Book now
                                            </Link>
                                        </div>
                                    </div>
                                ))}
                            </div>
                        )}

                        {servicesStyle === 'marquee' && (
                            <div
                                className="marquee -mx-6"
                                role="list"
                                aria-label="Our services"
                            >
                                {[0, 1].map((rowIndex) => (
                                    <div
                                        key={rowIndex}
                                        className={cn(
                                            'marquee-row',
                                            rowIndex === 1 &&
                                                'marquee-row-reverse',
                                        )}
                                        style={{
                                            animationDuration: `${packages.length * MARQUEE_SECONDS_PER_PACKAGE}s`,
                                        }}
                                    >
                                        {[...packages, ...packages].map(
                                            (pkg, index) => (
                                                <span
                                                    key={`${rowIndex}-${pkg.id}-${index}`}
                                                    role="listitem"
                                                    aria-hidden={
                                                        index >=
                                                            packages.length ||
                                                        rowIndex === 1
                                                            ? 'true'
                                                            : undefined
                                                    }
                                                    className={cn(
                                                        'inline-flex items-center rounded-full border px-5 py-2.5 text-sm font-semibold whitespace-nowrap',
                                                        pkg.is_featured
                                                            ? 'border-primary bg-primary/5 text-primary'
                                                            : 'border-border bg-muted/20 text-foreground',
                                                    )}
                                                >
                                                    {pkg.name}
                                                </span>
                                            ),
                                        )}
                                    </div>
                                ))}
                            </div>
                        )}

                        <div className="mt-8 text-center">
                            <p className="text-sm text-muted-foreground">
                                Prices vary by vehicle size. Add-ons available
                                at checkout.
                            </p>
                        </div>
                    </div>
                </section>
                </>
                )}

                <Separator />

                {/* ── Trust signals ── */}
                <section className="px-6 py-14">
                    <div className="mx-auto max-w-3xl">
                        <div className="grid grid-cols-2 gap-5 md:grid-cols-4">
                            {TRUST.map(({ icon: Icon, text }) => (
                                <div
                                    key={text}
                                    className="flex flex-col items-center gap-2 text-center"
                                >
                                    <div className="flex size-12 items-center justify-center rounded-full bg-primary/10">
                                        <Icon className="size-6 text-primary" />
                                    </div>
                                    <span className="text-sm leading-snug font-medium">
                                        {text}
                                    </span>
                                </div>
                            ))}
                        </div>
                    </div>
                </section>

                <Separator />

                {/* ── Bottom CTA ── */}
                <section className="px-6 py-16 text-center">
                    <div className="mx-auto max-w-xl">
                        <h2 className="text-3xl font-bold">
                            Ready for a clean car?
                        </h2>
                        <p className="mt-3 text-lg text-muted-foreground">
                            Book in 2 minutes. No account needed.
                        </p>
                        <Link
                            href="/book/location"
                            className={cn(
                                buttonVariants({ size: 'lg' }),
                                'mt-8 h-16 w-full max-w-sm gap-2 text-lg font-semibold',
                            )}
                        >
                            Get Started
                            <ArrowRight className="size-5" />
                        </Link>
                        <p className="mt-4 text-sm text-muted-foreground">
                            Questions? Call us at{' '}
                            <a
                                href="tel:+18005551234"
                                className="font-medium text-foreground hover:underline"
                            >
                                (800) 555-1234
                            </a>
                        </p>
                    </div>
                </section>

                {faq.categories.length > 0 && (
                    <>
                        <Separator />
                        <FaqSection faq={faq} />
                    </>
                )}

                {/* ── Footer ── */}
                <footer className="border-t px-6 py-8">
                    <div className="mx-auto flex max-w-5xl flex-col items-center justify-between gap-4 text-sm text-muted-foreground sm:flex-row">
                        <div className="flex items-center gap-2">
                            <Car className="size-4" />
                            <span className="font-medium text-foreground">
                                AIO Car Wash
                            </span>
                            <span>· Professional car wash service</span>
                        </div>
                        <div className="flex items-center gap-4">
                            {auth?.user ? (
                                <Link
                                    href={isStaff ? dashboard() : '/portal'}
                                    className="hover:text-foreground"
                                >
                                    My Account
                                </Link>
                            ) : (
                                <Link
                                    href={login.url()}
                                    className="hover:text-foreground"
                                >
                                    Sign in
                                </Link>
                            )}
                            <Link
                                href="/book/location"
                                className="hover:text-foreground"
                            >
                                Book now
                            </Link>
                            <span>© {new Date().getFullYear()}</span>
                        </div>
                    </div>
                </footer>
            </div>
        </>
    );
}
