import { useRef } from 'react';
import { Aurora } from '@/site/components/art/aurora';
import { ContourArt } from '@/site/components/art/contour-art';
import { Spotlight } from '@/site/components/motion/spotlight';
import { ButtonLink } from '@/site/components/ui/button';
import { useAmbient } from '@/site/hooks/use-in-view';
import { useShared } from '@/site/hooks/use-shared';
import { useT } from '@/site/i18n';

type ErrorProps = {
    status: number;
    title: string;
    description: string;
};

/**
 * Localised error page (404, 403, 500, 503…) — rendered through
 * App\Http\ErrorPages with the real HTTP status code.
 *
 * The status is set very large; every zero in it is a set of contour rings
 * that turns slowly and leans with the mouse (`.lost-code` in site.css) —
 * the site's own motif standing in for "nothing here".
 */
export default function ErrorPage({ status, title, description }: ErrorProps) {
    const { nav } = useShared();
    const t = useT();
    const code = useRef<HTMLParagraphElement>(null);

    useAmbient(code);

    return (
        <section className="stage-dark relative isolate flex min-h-svh items-center overflow-clip">
            <Aurora grid />
            <Spotlight />

            <div className="shell relative flex flex-col items-start gap-8 py-[calc(var(--header-h)+var(--topbar-h)+4rem)]">
                <p
                    ref={code}
                    data-ambient=""
                    data-pointer=""
                    className="hero-rise lost-code font-display text-accent"
                >
                    <span className="sr-only">{status}</span>
                    <span aria-hidden="true" className="flex items-center">
                        {String(status)
                            .split('')
                            .map((digit, index) =>
                                digit === '0' ? (
                                    <span key={index} className="lost-zero">
                                        <ContourArt
                                            seed={`lost-${index}`}
                                            rings={9}
                                            alive
                                        />
                                    </span>
                                ) : (
                                    <span key={index}>{digit}</span>
                                ),
                            )}
                    </span>
                </p>
                <h1 className="hero-rise font-display text-display-3">
                    {title}
                </h1>
                <p className="hero-rise text-lead text-soft max-w-xl">
                    {description}
                </p>
                <div className="hero-rise flex flex-wrap gap-4">
                    <ButtonLink href={nav.pages.home.url} size="lg">
                        {t.error.home}
                    </ButtonLink>
                    <ButtonLink
                        href={nav.pages.treatments.url}
                        size="lg"
                        variant="outline"
                        arrow={false}
                    >
                        {t.error.treatments}
                    </ButtonLink>
                </div>
            </div>
        </section>
    );
}
