import type { CSSProperties } from 'react';
import { Aurora } from '@/site/components/art/aurora';
import { Reveal } from '@/site/components/motion/reveal';
import { ViewScene } from '@/site/components/motion/scene';
import { Spotlight } from '@/site/components/motion/spotlight';
import { ButtonAnchor, ButtonLink } from '@/site/components/ui/button';
import { Em } from '@/site/components/ui/em';
import { Phone } from '@/site/components/ui/icons';
import { WhatsAppButton } from '@/site/components/ui/whatsapp-button';
import { useShared } from '@/site/hooks/use-shared';
import { useT } from '@/site/i18n';
import type { Accent } from '@/site/types';

const ring = (index: number) => ({ '--i': index }) as CSSProperties;

type CtaBandProps = {
    title?: string;
    text?: string;
    /** Where the main button leads; defaults to the contact page. */
    href?: string;
    accent?: Accent;
};

/**
 * Closing call to action shown at the end of most pages. The phone
 * shortcut appears only once the number is entered in the site settings;
 * the WhatsApp one opens the short form first while the CRM forms are on
 * (WhatsAppButton). While it scrolls into view the rings behind it open up
 * and the headline comes closer (`.cta-ring`, `.cta-title` in site.css).
 */
export function CtaBand({ title, text, href, accent }: CtaBandProps) {
    const { nav, site } = useShared();
    const t = useT();

    return (
        <ViewScene
            as="section"
            end={0.45}
            smooth={90}
            data-accent={accent}
            className="stage-dark relative isolate overflow-clip"
        >
            <Aurora intensity={0.9} />
            <Spotlight />

            {/* Rings that open up as the section scrolls in. */}
            <div
                aria-hidden="true"
                className="pointer-events-none absolute inset-0 grid place-items-center"
            >
                <div className="cta-ring" style={ring(0)} />
                <div className="cta-ring" style={ring(1)} />
                <div className="cta-ring" style={ring(2)} />
            </div>

            <div className="shell-narrow section-y relative flex flex-col items-center gap-8 text-center">
                <Reveal>
                    <h2 className="cta-title display-em font-display text-display-2">
                        <Em text={title ?? t.cta.title} />
                    </h2>
                </Reveal>
                <Reveal delay={80}>
                    <p className="text-lead text-soft max-w-xl">
                        {text ?? t.cta.text}
                    </p>
                </Reveal>
                <Reveal
                    delay={160}
                    className="flex flex-wrap items-center justify-center gap-4"
                >
                    <ButtonLink href={href ?? nav.pages.contact.url} size="lg">
                        {t.common.appointmentLong}
                    </ButtonLink>
                    <WhatsAppButton size="lg" />
                    {site.contact.phone && site.contact.phoneHref ? (
                        <ButtonAnchor
                            href={site.contact.phoneHref}
                            variant="ghost"
                            arrow={false}
                        >
                            <span className="inline-flex items-center gap-2.5">
                                <Phone className="size-5" />
                                {site.contact.phone}
                            </span>
                        </ButtonAnchor>
                    ) : null}
                </Reveal>
            </div>
        </ViewScene>
    );
}
