import { useRef } from 'react';
import type { CSSProperties } from 'react';
import { useAmbient } from '@/site/hooks/use-in-view';
import { cx } from '@/site/lib/cx';

type AuroraProps = {
    /** Overall strength of the glow, 0–1. */
    intensity?: number;
    /** Draw the fine grid lines behind the glow. */
    grid?: boolean;
    className?: string;
};

const blob = (color: string): CSSProperties => ({
    background: `radial-gradient(closest-side, ${color} 0%, transparent 72%)`,
});

/**
 * Ambient backdrop of dark sections: three soft colour fields drifting
 * slowly, tinted by the nearest [data-accent]. It stands in for hero
 * photography / video until real media arrives (docs/DESIGN_SYSTEM.md).
 *
 * Only `transform` is animated, the gradients are pre-softened (no blur
 * filter), and the loop pauses while the element is off screen.
 *
 * The fields are placed with `translate` in container units instead of
 * top/right/bottom offsets: when the section changes height (a web font
 * arriving and re-wrapping the heading, say) they follow through a
 * transform, which browsers do not count as a layout shift (CLS).
 */
export function Aurora({
    intensity = 1,
    grid = false,
    className,
}: AuroraProps) {
    const ref = useRef<HTMLDivElement>(null);

    useAmbient(ref);

    return (
        <div
            ref={ref}
            data-ambient=""
            aria-hidden="true"
            className={cx(
                'pointer-events-none absolute inset-0 overflow-hidden',
                className,
            )}
        >
            <div
                className="[container-type:size] absolute inset-0"
                style={{ opacity: intensity }}
            >
                {/* Top left: 25 % of the width and 45 % of the height outside. */}
                <div
                    className="animate-drift-a absolute top-0 left-0 size-[95vmax] translate-x-[-25cqw] translate-y-[-45cqh] opacity-45"
                    style={blob('var(--accent)')}
                />
                {/* Top right: sticking out 35 % of the width on the right. */}
                <div
                    className="animate-drift-b absolute top-0 left-0 size-[85vmax] translate-x-[calc(135cqw-85vmax)] translate-y-[-10cqh] opacity-40"
                    style={blob('var(--accent-2)')}
                />
                {/* Bottom: hanging 55 % of the height below the section. */}
                <div
                    className="animate-drift-c absolute top-0 left-0 size-[90vmax] translate-x-[10cqw] translate-y-[calc(155cqh-90vmax)] opacity-70"
                    style={blob('var(--color-deep)')}
                />
            </div>
            {grid ? (
                <div className="grid-lines absolute inset-0 opacity-70" />
            ) : null}
            {/* Vignette: keeps text areas calm and blends into the next section. */}
            <div className="absolute inset-0 bg-[radial-gradient(ellipse_120%_90%_at_50%_35%,transparent_35%,var(--color-ink)_100%)]" />
            <div className="grain absolute inset-0" />
        </div>
    );
}
