import { useLayoutEffect, useRef } from 'react';
import Logo from '../../assets/chowman-bg-logo.avif';
import Alpona from '../../assets/chowman_alpona.avif';
import Thinking from '../../assets/thinking.avif';
import { Head, Link } from '@inertiajs/react';
import { ArrowRight } from 'lucide-react';

// Same alpona sizing as Register / Verify so the screens feel related.
const ALPONA_SIZE = 'min(64vw,300px,30dvh)';

/** Mascot that rises up from the bottom edge and settles. */
const PeekingMascot = () => {
    const ref = useRef<HTMLImageElement>(null);

    useLayoutEffect(() => {
        const el = ref.current;
        if (!el) return;
        if (window.matchMedia('(prefers-reduced-motion: reduce)').matches)
            return;

        const animation = el.animate(
            [{ transform: 'translateY(100%)' }, { transform: 'translateY(0)' }],
            {
                duration: 1400,
                delay: 300,
                easing: 'cubic-bezier(0.22, 1, 0.36, 1)', // fast start, soft landing
                fill: 'backwards', // stay hidden during the delay
            },
        );

        return () => animation.cancel();
    }, []);

    return (
        <img
            ref={ref}
            src={Thinking}
            alt=""
            className="pointer-events-none absolute bottom-0 left-1/2 h-full max-h-44 w-auto max-w-none -translate-x-1/2 select-none"
        />
    );
};

const NotFound = () => {
    return (
        <>
            <Head title="Page not found" />

            <main className="flex min-h-[100dvh] flex-col overflow-x-hidden px-5 py-[clamp(12px,3dvh,32px)] sm:px-8 sm:py-8 lg:px-12">
                <div className="mx-auto flex w-full max-w-[700px] flex-1 flex-col items-center">
                    {/* Logo */}
                    <div className="flex shrink-0 justify-center">
                        <img
                            src={Logo}
                            className="h-[clamp(28px,5dvh,48px)] w-auto sm:h-12"
                            alt="Chowman Logo"
                        />
                    </div>

                    {/* 404 over the spinning alpona */}
                    <div
                        className="relative mt-[clamp(8px,3dvh,40px)] flex shrink-0 items-center justify-center"
                        style={{ width: ALPONA_SIZE, height: ALPONA_SIZE }}
                    >
                        <img
                            src={Alpona}
                            alt=""
                            className="pointer-events-none absolute inset-0 h-full w-full animate-[spin_40s_linear_infinite] object-contain opacity-40 select-none motion-reduce:animate-none"
                        />
                        <span
                            className="relative z-10 font-amethyst leading-none tracking-widest text-[#FFF8EA]"
                            style={{ fontSize: `calc(${ALPONA_SIZE} * 0.42)` }}
                        >
                            404
                        </span>
                    </div>

                    {/* Message */}
                    <div className="relative z-10 mt-[clamp(12px,3dvh,40px)] shrink-0 text-center">
                        <h1 className="font-amethyst text-[clamp(1.75rem,4.5dvh,3.25rem)] leading-[1.1] text-[#FFF8EA]">
                            <span className="tracking-wider">
                                This page doesn't exist
                            </span>
                        </h1>

                        <p className="mx-auto mt-[clamp(8px,1.8dvh,20px)] max-w-[420px] font-montserrat text-xs leading-[1.4] text-[#C5A98B] sm:text-[19px]">
                            The link may be broken, or the page may have moved.
                            Head back to the start to keep playing.
                        </p>
                    </div>

                    {/* Mascot fills the leftover height; never smaller than 80px */}
                    <div className="relative mt-2 min-h-[80px] w-full flex-1 overflow-hidden">
                        <PeekingMascot />
                    </div>

                    {/* Back home */}
                    <Link
                        href="/"
                        className="relative z-10 mt-2 flex h-[clamp(44px,7dvh,84px)] w-full shrink-0 items-center justify-center gap-2 rounded-xl border border-[#F0D28E] bg-gradient-to-b from-[#E7C27A] to-[#CEA052] font-montserrat text-sm font-bold text-[#230505] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#FFF8EA] sm:text-[22px] lg:text-[27px]"
                    >
                        Back to home
                        <ArrowRight className="h-5 w-5" />
                    </Link>
                </div>
            </main>
        </>
    );
};

export default NotFound;