import { Head, Link, usePage } from '@inertiajs/react';
import { ArrowLeft, ArrowRight, ChevronRight, Leaf, Sprout } from 'lucide-react';
import { useState } from 'react';
import ImageLightbox from '@/components/frontend/image-lightbox';
import { Button } from '@/components/ui/button';
import { ServiceIcon } from '@/components/frontend/ServiceIcon';

type Step = {
    step_number: number;
    icon: string | null;
    title: string;
    slug: string | null;
    description: string | null;
    image: string | null;
    gallery: { id: number; url: string; thumb: string; original: string }[];
};

type OtherStep = { step_number: number; title: string; slug: string | null };

type Props = {
    step: Step;
    others: OtherStep[];
    seo: { title: string; description: string; image: string | null };
};

export default function ProcessDetail({ step, others, seo }: Props) {
    const { props } = usePage();
    const locale = String(props.locale ?? 'id');
    const isEn = locale === 'en';
    const [lightboxIndex, setLightboxIndex] = useState<number | null>(null);
    const galleryImages = step.gallery && step.gallery.length > 0 ? step.gallery.map((g) => ({ id: g.id, url: g.original, alt: step.title })) : step.image ? [{ id: 0, url: step.image, alt: step.title }] : [];

    // Find prev/next steps
    const sortedOthers = [...others].sort((a, b) => a.step_number - b.step_number);
    const currentIdx = sortedOthers.findIndex((o) => o.step_number === step.step_number);
    const prevStep = currentIdx > 0 ? sortedOthers[currentIdx - 1] : null;
    const nextStep = currentIdx < sortedOthers.length - 1 ? sortedOthers[currentIdx + 1] : null;

    return (
        <>
            <Head title={seo.title}>
                <meta name="description" content={seo.description} />
                {seo.image && <meta property="og:image" content={seo.image} />}
            </Head>

            {/* Hero — immersive with image background */}
            <section className="relative overflow-hidden bg-[#FFF9F5] dark:bg-[#0F0A07]">
                <div className="pointer-events-none absolute -top-20 -right-20 size-96 rounded-full bg-[#A05A2C]/10 blur-3xl" />
                <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8 lg:py-12">
                    {/* Breadcrumb */}
                    <nav className="flex items-center gap-1.5 text-xs text-[#6B5B4F]/70 dark:text-white/50">
                        <Link href={`/${locale}`} className="transition hover:text-[#A05A2C]">
                            {isEn ? 'Home' : 'Beranda'}
                        </Link>
                        <ChevronRight className="size-3" />
                        <Link href={`/${locale}/proses-kami`} className="transition hover:text-[#A05A2C]">
                            {isEn ? 'Our Process' : 'Proses Kami'}
                        </Link>
                        <ChevronRight className="size-3" />
                        <span className="font-medium text-[#2B1B12] dark:text-white">{step.title}</span>
                    </nav>

                    {/* Title area */}
                    <div className="mt-8 max-w-3xl">
                        <div className="flex items-center gap-3">
                            {step.icon && (
                                <div className="flex size-10 items-center justify-center rounded-xl bg-[#6B2F0B] text-white shadow-md">
                                    <ServiceIcon name={step.icon} className="size-5" />
                                </div>
                            )}
                            <span className="inline-flex items-center gap-1.5 rounded-full bg-[#6B2F0B] px-3 py-1 text-xs font-bold text-white">
                                {isEn ? 'Step' : 'Langkah'} {step.step_number}
                            </span>
                        </div>
                        <h1 className="mt-4 font-poppins text-3xl font-bold leading-[1.1] tracking-tight text-[#2B1B12] sm:text-4xl dark:text-white">
                            {step.title}
                        </h1>
                        <div className="mt-3 h-0.5 w-16 rounded bg-[#A05A2C]" />
                    </div>
                </div>
            </section>

            {/* Content — two-column layout */}
            <section className="bg-white dark:bg-[#0F0A07]">
                <div className="mx-auto max-w-7xl px-4 py-10 sm:px-6 lg:px-8 lg:py-14">
                    <div className="grid gap-10 lg:grid-cols-5 lg:gap-14">
                        {/* Main content */}
                        <div className="min-w-0 lg:col-span-3">
                            {/* Gallery */}
                            <div className="space-y-3">
                                <div className="relative overflow-hidden rounded-[24px]">
                                    {step.gallery && step.gallery.length > 0 ? (
                                        <button type="button" onClick={() => setLightboxIndex(0)} className="block w-full">
                                            <img
                                                src={step.gallery[0].url}
                                                alt={step.title}
                                                loading="lazy"
                                                className="aspect-[16/10] w-full object-cover transition hover:opacity-95"
                                            />
                                        </button>
                                    ) : step.image ? (
                                        <button type="button" onClick={() => setLightboxIndex(0)} className="block w-full">
                                            <img
                                                src={step.image}
                                                alt={step.title}
                                                loading="lazy"
                                                className="aspect-[16/10] w-full object-cover transition hover:opacity-95"
                                            />
                                        </button>
                                    ) : (
                                        <div className="flex aspect-[16/10] w-full items-center justify-center bg-gradient-to-br from-[#A05A2C]/15 to-[#6B2F0B]/10">
                                            <Sprout className="size-16 text-[#A05A2C]/30" />
                                        </div>
                                    )}
                                    <div className="pointer-events-none absolute inset-0 bg-gradient-to-t from-black/10 via-transparent to-transparent" />
                                </div>
                                {step.gallery && step.gallery.length > 1 && (
                                    <div className="grid grid-cols-3 gap-2 sm:grid-cols-4">
                                        {step.gallery.map((g, idx) => (
                                            <button key={g.id} type="button" onClick={() => setLightboxIndex(idx)} className="group relative overflow-hidden rounded-xl border">
                                                <img src={g.thumb} alt="" className="aspect-[4/3] w-full object-cover transition-transform group-hover:scale-105" loading="lazy" />
                                            </button>
                                        ))}
                                    </div>
                                )}
                                <ImageLightbox images={galleryImages} openIndex={lightboxIndex} onClose={() => setLightboxIndex(null)} onNavigate={setLightboxIndex} locale={locale} />
                            </div>

                            {/* Description */}
                            {step.description && (
                                <div
                                    className="tiptap-content mt-8 text-sm leading-relaxed text-[#6B5B4F] dark:text-white/80 [&_h2]:mt-8 [&_h2]:font-poppins [&_h2]:text-lg [&_h2]:font-semibold [&_h2]:text-[#2B1B12] [&_h2]:dark:text-white [&_h3]:mt-6 [&_h3]:font-poppins [&_h3]:text-base [&_h3]:font-semibold [&_h3]:text-[#2B1B12] [&_h3]:dark:text-white [&_li]:my-1 [&_p]:my-3 [&_strong]:font-semibold [&_strong]:text-[#2B1B12] [&_strong]:dark:text-white"
                                    dangerouslySetInnerHTML={{ __html: step.description }}
                                />
                            )}

                            {/* Navigation buttons */}
                            <div className="mt-10 flex flex-wrap items-center gap-3">
                                <Button asChild variant="outline" className="rounded-xl">
                                    <Link href={`/${locale}/proses-kami`}>
                                        <ArrowLeft className="size-4" /> {isEn ? 'All steps' : 'Semua tahap'}
                                    </Link>
                                </Button>
                                {nextStep?.slug ? (
                                    <Button asChild className="rounded-xl bg-[#6B2F0B] hover:bg-[#A05A2C]">
                                        <Link href={`/${locale}/proses-kami/${nextStep.slug}`}>
                                            {isEn ? 'Next' : 'Selanjutnya'} <ArrowRight className="size-4" />
                                        </Link>
                                    </Button>
                                ) : (
                                    prevStep?.slug ? (
                                        <Button asChild variant="outline" className="rounded-xl">
                                            <Link href={`/${locale}/proses-kami/${prevStep.slug}`}>
                                                <ArrowLeft className="size-4" /> {isEn ? 'Previous' : 'Sebelumnya'}
                                            </Link>
                                        </Button>
                                    ) : null
                                )}
                            </div>
                        </div>

                        {/* Sidebar — other steps */}
                        <div className="lg:col-span-2">
                            <div className="lg:sticky lg:top-24">
                                <h2 className="font-poppins text-base font-semibold text-[#2B1B12] dark:text-white">
                                    {isEn ? 'All Process Steps' : 'Semua Tahapan'}
                                </h2>
                                <div className="mt-1 h-0.5 w-8 rounded bg-[#A05A2C]" />

                                <div className="mt-5 space-y-2">
                                    {/* Current step */}
                                    <div className="flex items-center gap-3 rounded-xl bg-[#6B2F0B] p-3 text-white shadow-md">
                                        <span className="flex size-8 shrink-0 items-center justify-center rounded-lg bg-white/15 text-xs font-bold">
                                            {step.step_number}
                                        </span>
                                        <span className="font-poppins text-sm font-semibold">{step.title}</span>
                                    </div>

                                    {/* Other steps */}
                                    {sortedOthers
                                        .filter((o) => o.slug && o.step_number !== step.step_number)
                                        .map((o) => (
                                            <Link
                                                key={o.slug}
                                                href={`/${locale}/proses-kami/${o.slug}`}
                                                className="group flex items-center gap-3 rounded-xl border border-transparent p-3 transition-all duration-300 hover:border-[#A05A2C]/10 hover:bg-[#FFF9F5] hover:shadow-sm dark:hover:bg-white/[0.04]"
                                            >
                                                <span className="flex size-8 shrink-0 items-center justify-center rounded-lg bg-[#A05A2C]/10 text-xs font-bold text-[#A05A2C] transition-colors group-hover:bg-[#6B2F0B] group-hover:text-white">
                                                    {o.step_number}
                                                </span>
                                                <span className="line-clamp-1 font-poppins text-sm font-medium text-[#6B5B4F] group-hover:text-[#2B1B12] dark:text-white/70 dark:group-hover:text-white">
                                                    {o.title}
                                                </span>
                                                <ChevronRight className="ml-auto size-3.5 text-[#A05A2C]/40 opacity-0 transition-opacity group-hover:opacity-100" />
                                            </Link>
                                        ))}
                                </div>

                                {/* CTA card */}
                                <div className="mt-6 overflow-hidden rounded-2xl bg-[#6B2F0B] p-5 text-white">
                                    <div className="pointer-events-none absolute inset-0 opacity-[0.08]" style={{ backgroundImage: 'radial-gradient(rgba(255,255,255,0.7) 1px, transparent 1px)', backgroundSize: '18px 18px' }} />
                                    <p className="relative font-poppins text-sm font-semibold">
                                        {isEn ? 'Interested in our process?' : 'Tertarik dengan proses kami?'}
                                    </p>
                                    <p className="relative mt-1 text-xs text-white/75">
                                        {isEn ? 'Contact us for more details.' : 'Hubungi kami untuk informasi lebih lanjut.'}
                                    </p>
                                    <Button asChild size="sm" className="relative mt-3 bg-white text-[#6B2F0B] hover:bg-white/90">
                                        <Link href={`/${locale}/kontak`}>
                                            {isEn ? 'Contact Us' : 'Hubungi Kami'} <ArrowRight className="size-3.5" />
                                        </Link>
                                    </Button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            {/* CTA — consistent with process list */}
            <section className="bg-[#FFF9F5] px-4 py-10 sm:px-6 lg:px-8 lg:py-12 dark:bg-[#0F0A07]">
                <div className="relative mx-auto max-w-7xl overflow-hidden rounded-[28px] bg-[#6B2F0B] p-8 sm:p-10 dark:bg-[#2A1A0F]">
                    <div className="pointer-events-none absolute inset-0 opacity-[0.10]" style={{ backgroundImage: 'radial-gradient(rgba(255,255,255,0.7) 1px, transparent 1px)', backgroundSize: '22px 22px' }} />
                    <Leaf className="pointer-events-none absolute -top-10 -right-10 size-56 rotate-12 text-white/[0.06]" />
                    <Leaf className="pointer-events-none absolute -bottom-14 -left-14 size-64 -rotate-12 text-white/[0.05]" />
                    <div className="relative grid items-center gap-6 lg:grid-cols-5">
                        <div className="lg:col-span-3">
                            <h2 className="font-poppins text-xl font-semibold text-white sm:text-2xl">
                                {isEn ? 'Ready to Be Your Business Partner' : 'Siap Menjadi Mitra Bisnis Anda'}
                            </h2>
                            <p className="mt-2 max-w-xl text-sm leading-relaxed text-white/80">
                                {isEn
                                    ? "Let's collaborate and grow together. Contact us for the best offer and more info about your cocoa needs."
                                    : 'Mari berkolaborasi dan tumbuh bersama. Hubungi kami untuk penawaran terbaik dan informasi lebih lanjut tentang kebutuhan cocoa Anda.'}
                            </p>
                        </div>
                        <div className="flex flex-wrap gap-3 lg:col-span-2 lg:justify-end">
                            <Button asChild size="lg" className="bg-white text-[#6B2F0B] hover:bg-white/90">
                                <Link href={`/${locale}/kontak`}>
                                    {isEn ? 'Contact Us Now' : 'Hubungi Kami Sekarang'} <ArrowRight className="size-4" />
                                </Link>
                            </Button>
                            <Button asChild variant="outline" size="lg" className="border-white bg-transparent text-white hover:bg-white hover:text-[#6B2F0B]">
                                <Link href={`/${locale}/produk`}>{isEn ? 'View Products' : 'Lihat Produk'}</Link>
                            </Button>
                        </div>
                    </div>
                </div>
            </section>
        </>
    );
}
