import type { CSSProperties } from 'react';
import { Aurora } from '@/site/components/art/aurora';
import { HeroField } from '@/site/components/art/hero-field';
import { MediaSlot } from '@/site/components/media/media-slot';
import { Scrub, ViewScene } from '@/site/components/motion/scene';
import { HeroBackdrop } from '@/site/components/sections/home/hero-backdrop';
import { ButtonAnchor } from '@/site/components/ui/button';
import { Em } from '@/site/components/ui/em';
import { Eyebrow } from '@/site/components/ui/heading';
import { useT } from '@/site/i18n';
import type { Media } from '@/site/types';

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

type HomeHeroProps = {
    /** Background video or photo replacing the artwork, once the clinic has one. */
    media: Media | null;
    /** Faint photos of the clinic behind the artwork (media/home/backdrop-*). */
    backdrops: Media[];
};

/**
 * Opening screen of the home page (docs/DECISIONS.md K45, K46). It says
 * what Medical Longevity means for the visitor — not which departments the
 * clinic has: those follow right below.
 *
 * - The eyebrow names the approach, the heading turns it into a message,
 *   the lead says what the personal health plan looks after (the clinic's
 *   own words, K47; how every treatment starts follows in the next section).
 * - Nothing beside it: the numbered steps and the counts that stood there
 *   read as machine-made to the clinic (K46), and the memberships strip at
 *   the bottom was dropped at its request.
 * - The second button leads to the process section further down the page.
 *
 * The text enters with CSS only (no JavaScript needed for the first paint)
 * and falls behind as the visitor starts to scroll, while the next section
 * slides over it like a sheet.
 *
 * Behind the text, from back to front: faint photos of the clinic fading
 * into one another, the CSS aurora, and the "living contours" field
 * (WebGL, see HeroField) whose ground lets the photos show through. The
 * backdrop is masked where the text sits (`.hero-backdrop` in site.css):
 * on a phone the text covers the whole width, so the whole of it calms
 * down. A video or photo in media/home/hero.* replaces all three.
 */
export function HomeHero({ media, backdrops }: HomeHeroProps) {
    const t = useT();
    const copy = t.home.hero;

    return (
        <ViewScene
            as="section"
            start={0}
            end={0}
            className="stage-dark relative isolate flex min-h-svh flex-col overflow-clip pb-12 md:pb-16"
        >
            <MediaSlot
                media={media}
                priority
                className="absolute inset-0 -z-10"
                fallback={
                    <div className="hero-backdrop absolute inset-0 -z-10">
                        <HeroBackdrop photos={backdrops} />
                        <Aurora grid intensity={backdrops.length ? 0.7 : 1} />
                        <HeroField ground={backdrops.length ? 0.5 : 1} />
                        {/* Keeps the text calm whatever is drawn behind it. */}
                        <div
                            aria-hidden="true"
                            className="from-ink via-ink/60 absolute inset-0 bg-linear-to-t to-transparent"
                        />
                        <div
                            aria-hidden="true"
                            className="from-ink/70 absolute inset-0 bg-linear-to-r to-transparent lg:to-60%"
                        />
                    </div>
                }
            />
            {media ? (
                // Keeps the text readable over any footage.
                <div
                    aria-hidden="true"
                    className="from-ink via-ink/70 to-ink/45 absolute inset-0 -z-10 bg-linear-to-t"
                />
            ) : null}

            <Scrub
                to={0.8}
                opacity={[1, 0]}
                y={[0, 140]}
                scale={[1, 0.94]}
                className="shell relative flex grow flex-col justify-end pt-[calc(var(--header-h)+var(--topbar-h)+3rem)] pb-10 md:pb-14"
            >
                <div className="max-w-5xl">
                    {/* The approach's name: a step larger than other eyebrows. */}
                    <div className="hero-rise">
                        <Eyebrow className="md:text-sm">{copy.eyebrow}</Eyebrow>
                    </div>
                    {/*
                     * Plain wrapping instead of the headings' balanced one:
                     * full lines, so the long title takes as few lines as
                     * possible on a phone (three on desktop, four on a
                     * 390 px screen) and the buttons stay on the first screen.
                     */}
                    <h1
                        className="hero-rise display-em font-display text-display-1 mt-6 text-wrap md:mt-7"
                        style={delay(120)}
                    >
                        <Em text={copy.title} />
                    </h1>
                    <p
                        className="hero-rise text-lead text-soft mt-7 max-w-2xl md:mt-8 lg:max-w-3xl"
                        style={delay(240)}
                    >
                        {copy.lead}
                    </p>
                    <div
                        className="hero-rise mt-9 flex flex-wrap gap-3 md:mt-10 md:gap-4"
                        style={delay(340)}
                    >
                        {/* The appointment request at the end of this page. */}
                        <ButtonAnchor href="#randevu" size="lg">
                            {copy.primary}
                        </ButtonAnchor>
                        {/* The process section further down. */}
                        <ButtonAnchor
                            href="#yaklasim"
                            size="lg"
                            variant="outline"
                            arrow={false}
                        >
                            {copy.secondary}
                        </ButtonAnchor>
                    </div>
                </div>
            </Scrub>
        </ViewScene>
    );
}
