import { useRef } from 'react';
import type { CSSProperties, ReactNode } from 'react';
import { Aurora } from '@/site/components/art/aurora';
import { Scrub, ViewScene } from '@/site/components/motion/scene';
import { Spotlight } from '@/site/components/motion/spotlight';
import { Breadcrumbs } from '@/site/components/ui/breadcrumbs';
import { Em } from '@/site/components/ui/em';
import { Eyebrow } from '@/site/components/ui/heading';
import { useAmbient } from '@/site/hooks/use-in-view';
import { useShared } from '@/site/hooks/use-shared';
import { cx } from '@/site/lib/cx';
import type { Accent } from '@/site/types';

type PageHeroProps = {
    /** The page's <h1>; *asterisks* mark the italic emphasis. */
    title: string;
    eyebrow?: ReactNode;
    lead?: string;
    /** Accent theme of the glow and emphasis. */
    accent?: Accent;
    /** A photo, video or their stand-in, shown beside the text (below it on small screens). */
    visual?: ReactNode;
    /**
     * A line drawing of the page (components/art): beside the text on large
     * screens, a faint ornament in the corner on small ones. Decorative —
     * it is laid over the hero and never changes its height.
     */
    art?: ReactNode;
    /** Extra content under the lead: buttons, quick facts… */
    children?: ReactNode;
    size?: 'display-1' | 'display-2';
};

const delay = (ms: number) => ({ '--delay': `${ms}ms` }) as CSSProperties;

/** Frame of a real photo or video in the `visual` slot (MediaSlot `className`). */
export const heroFrame =
    'aspect-[4/5] max-h-[72svh] w-full rounded-xl border border-line';

/**
 * The section right after the hero: a sheet that slides over it as the
 * page scrolls (the hero leaves room for the overlap).
 */
export const sheet = 'relative z-10 -mt-10 rounded-t-xl';

/**
 * Opening section of every inner page: breadcrumb trail, eyebrow, the one
 * <h1> of the page and a lead paragraph on the dark "stage", with the
 * page's photo or drawing beside them.
 *
 * Everything here enters with CSS only (`hero-rise`, and the strokes of a
 * drawing), so the first screen (and the LCP element) never waits for
 * JavaScript. As the page scrolls the text falls behind and the next
 * section slides over the hero like a sheet (`sheet`); under the mouse a
 * soft light lifts the grid and the drawing leans with it (Spotlight).
 */
export function PageHero({
    title,
    eyebrow,
    lead,
    accent,
    visual,
    art,
    children,
    size = 'display-2',
}: PageHeroProps) {
    const { breadcrumbs } = useShared();
    const artRef = useRef<HTMLDivElement>(null);

    // The loops of the drawing rest while the hero is off screen.
    useAmbient(artRef);

    return (
        <ViewScene
            as="section"
            start={0}
            end={0}
            data-accent={accent}
            className="stage-dark relative isolate overflow-clip"
        >
            <Aurora intensity={0.8} grid />
            <Spotlight />

            {art ? (
                <div aria-hidden="true" className="hero-art-slot">
                    <Scrub
                        to={0.9}
                        y={[0, -48]}
                        opacity={[1, 0.25]}
                        className="size-full"
                    >
                        <div
                            ref={artRef}
                            data-ambient=""
                            data-pointer=""
                            className="hero-art hero-fade size-full"
                            style={delay(150)}
                        >
                            {art}
                        </div>
                    </Scrub>
                </div>
            ) : null}

            <Scrub
                to={0.9}
                opacity={[1, 0.15]}
                y={[0, 96]}
                scale={[1, 0.97]}
                className="shell pointer-events-none relative grid items-end gap-12 pt-[calc(var(--header-h)+var(--topbar-h)+3.5rem)] pb-26 md:pt-[calc(var(--header-h)+var(--topbar-h)+5.5rem)] md:pb-34 lg:grid-cols-12 lg:gap-10"
            >
                <div
                    className={cx(
                        'pointer-events-auto flex flex-col gap-7',
                        visual || art ? 'lg:col-span-7' : 'lg:col-span-9',
                    )}
                >
                    <Breadcrumbs items={breadcrumbs} className="hero-rise" />

                    {eyebrow ? (
                        <div className="hero-rise" style={delay(60)}>
                            <Eyebrow as="div">{eyebrow}</Eyebrow>
                        </div>
                    ) : null}

                    <h1
                        className={cx(
                            'hero-rise display-em font-display',
                            size === 'display-1'
                                ? 'text-display-1'
                                : 'text-display-2',
                        )}
                        style={delay(120)}
                    >
                        <Em text={title} />
                    </h1>

                    {lead ? (
                        <p
                            className="hero-rise text-lead text-soft max-w-2xl"
                            style={delay(220)}
                        >
                            {lead}
                        </p>
                    ) : null}

                    {children ? (
                        <div className="hero-rise" style={delay(320)}>
                            {children}
                        </div>
                    ) : null}
                </div>

                {visual ? (
                    <div
                        data-pointer=""
                        className="hero-art hero-fade pointer-events-auto mx-auto w-full max-w-sm lg:col-span-5 lg:max-w-none"
                        style={delay(200)}
                    >
                        {visual}
                    </div>
                ) : null}
            </Scrub>
        </ViewScene>
    );
}
