import { useEffect, useRef } from 'react';

/**
 * "Lumière" — a premium, photography-forward wedding-site theme.
 *
 * Unlike the palette-only themes in w/show.tsx, this is a self-contained
 * *layout*: warm ivory paper, a high-contrast romantic serif (Cormorant
 * Garamond) against spaced geometric labels (Jost), champagne-gold hairlines
 * and ornaments, and gentle staggered reveals. It consumes the same section
 * data model as every other theme, so couples can switch to it with one click.
 */

interface Props {
    wedding: {
        partner_a_name: string;
        partner_b_name: string;
    };
    content: (id: string) => Record<string, string>;
    visible: (id: string) => boolean;
    timeline?: { id: string; title: string; start_time: string; location?: string }[];
    coupleNames: string;
    weddingDate: string | null;
    venue: string;
    t: (key: string, replacements?: Record<string, string | number>) => string;
}

// Monogram initials, e.g. "Amelia & Julian" → "A · J"
function monogram(a: string, b: string): string {
    const i = (s: string) => (s.trim()[0] ?? '').toUpperCase();
    return `${i(a)} · ${i(b)}`;
}

export default function LumiereTheme({ wedding, content, visible, timeline, coupleNames, weddingDate, venue, t }: Props) {
    const rootRef = useRef<HTMLDivElement>(null);

    // Reveal sections as they scroll into view (load-in stagger handles the hero).
    useEffect(() => {
        const els = rootRef.current?.querySelectorAll<HTMLElement>('[data-reveal]');
        if (!els || !els.length) return;
        const io = new IntersectionObserver(
            (entries) => entries.forEach((e) => {
                if (e.isIntersecting) { e.target.classList.add('is-in'); io.unobserve(e.target); }
            }),
            { rootMargin: '0px 0px -12% 0px', threshold: 0.12 },
        );
        els.forEach((el) => io.observe(el));
        return () => io.disconnect();
    }, []);

    const hero = content('hero');
    const heroImg = hero.backgroundUrl?.trim();
    const story = content('story');
    const details = content('details');
    const hasCeremony = !!details.ceremonyTime;
    const hasReception = !!details.receptionTime;
    const rsvp = content('rsvp');
    const registry = content('registry');
    const faq = content('faq');

    const partnerA = wedding.partner_a_name;
    const partnerB = wedding.partner_b_name;

    return (
        <div ref={rootRef} className="lumiere">
            {/* React 19 hoists these to <head> and dedupes — fonts load (non-blocking)
                only when this theme actually renders, with preconnect for speed. */}
            <link rel="preconnect" href="https://fonts.googleapis.com" />
            <link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="anonymous" />
            <link
                rel="stylesheet"
                href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,500;0,600;1,400;1,500&family=Jost:wght@300;400;500&display=swap"
            />
            <style>{LUMIERE_CSS}</style>

            {/* ── Hero ─────────────────────────────────────────────── */}
            {visible('hero') && (
                <header
                    className={`lum-hero${heroImg ? ' lum-hero--photo' : ''}`}
                    style={heroImg ? { backgroundImage: `url(${heroImg})` } : undefined}
                >
                    {heroImg && <div className="lum-hero__veil" aria-hidden="true" />}
                    <div className="lum-hero__inner">
                        <p className="lum-eyebrow lum-fade" style={{ animationDelay: '0.15s' }}>
                            <span className="lum-rule" />
                            {t('wsite.eyebrow_wedding')}
                            <span className="lum-rule" />
                        </p>

                        <h1 className="lum-names lum-fade" style={{ animationDelay: '0.3s' }}>
                            <span className="lum-names__a">{partnerA}</span>
                            <span className="lum-amp">&amp;</span>
                            <span className="lum-names__b">{partnerB}</span>
                        </h1>

                        {weddingDate && (
                            <p className="lum-meta lum-fade" style={{ animationDelay: '0.5s' }}>{weddingDate}</p>
                        )}
                        {venue && (
                            <p className="lum-venue lum-fade" style={{ animationDelay: '0.6s' }}>{venue}</p>
                        )}
                        {hero.subheading && (
                            <p className="lum-sub lum-fade" style={{ animationDelay: '0.72s' }}>{hero.subheading}</p>
                        )}
                    </div>

                    <span className="lum-scroll lum-fade" style={{ animationDelay: '1s' }}>{t('wsite.scroll')}</span>
                </header>
            )}

            {/* ── Story ────────────────────────────────────────────── */}
            {visible('story') && story.body && (
                <section className="lum-section lum-section--narrow" data-reveal>
                    <Ornament />
                    <p className="lum-eyebrow lum-eyebrow--dark">{t('wsite.chapter_1')}</p>
                    <h2 className="lum-h2">{story.heading || t('wsite.our_story')}</h2>
                    <p className="lum-prose">{story.body}</p>
                </section>
            )}

            {/* ── Details ──────────────────────────────────────────── */}
            {visible('details') && (hasCeremony || hasReception) && (
                <section className="lum-section" data-reveal>
                    <Ornament />
                    <p className="lum-eyebrow lum-eyebrow--dark">{t('wsite.chapter_2')}</p>
                    <h2 className="lum-h2 lum-h2--center">{details.heading || t('wsite.event_details')}</h2>
                    <div className={`lum-details${hasCeremony && hasReception ? '' : ' lum-details--single'}`}>
                        {hasCeremony && (
                            <article className="lum-detail">
                                <p className="lum-detail__label">{t('wsite.ceremony')}</p>
                                <p className="lum-detail__time">{details.ceremonyTime}</p>
                                {details.ceremonyVenue && <p className="lum-detail__venue">{details.ceremonyVenue}</p>}
                            </article>
                        )}
                        {hasCeremony && hasReception && <span className="lum-detail__divider" aria-hidden="true" />}
                        {hasReception && (
                            <article className="lum-detail">
                                <p className="lum-detail__label">{t('wsite.reception')}</p>
                                <p className="lum-detail__time">{details.receptionTime}</p>
                                {details.receptionVenue && <p className="lum-detail__venue">{details.receptionVenue}</p>}
                            </article>
                        )}
                    </div>
                </section>
            )}

            {/* ── Timeline ─────────────────────────────────────────── */}
            {visible('timeline') && timeline && timeline.length > 0 && (
                <section className="lum-section lum-section--narrow" data-reveal>
                    <Ornament />
                    <p className="lum-eyebrow lum-eyebrow--dark">{t('wsite.the_day')}</p>
                    <h2 className="lum-h2 lum-h2--center">{content('timeline').heading?.trim() || t('timeline.title')}</h2>
                    <ol className="lum-timeline">
                        {timeline.map((ev) => (
                            <li key={ev.id} className="lum-timeline__item">
                                <span className="lum-timeline__time">{ev.start_time}</span>
                                <span className="lum-timeline__node" aria-hidden="true" />
                                <span className="lum-timeline__body">
                                    <span className="lum-timeline__title">{ev.title}</span>
                                    {ev.location && <span className="lum-timeline__loc">{ev.location}</span>}
                                </span>
                            </li>
                        ))}
                    </ol>
                </section>
            )}

            {/* ── RSVP ─────────────────────────────────────────────── */}
            {visible('rsvp') && rsvp.body?.trim() && (
                <section className="lum-section lum-section--narrow lum-section--center" data-reveal>
                    <Ornament />
                    <p className="lum-eyebrow lum-eyebrow--dark">{t('wsite.reply')}</p>
                    <h2 className="lum-h2">{rsvp.heading?.trim() || t('rsvp.title')}</h2>
                    <p className="lum-prose">{rsvp.body}</p>
                </section>
            )}

            {/* ── Registry ─────────────────────────────────────────── */}
            {visible('registry') && registry.body?.trim() && (
                <section className="lum-section lum-section--narrow lum-section--center" data-reveal>
                    <Ornament />
                    <p className="lum-eyebrow lum-eyebrow--dark">{t('wsite.with_gratitude')}</p>
                    <h2 className="lum-h2">{registry.heading?.trim() || t('wsite.registry')}</h2>
                    <p className="lum-prose">{registry.body}</p>
                </section>
            )}

            {/* ── FAQ ──────────────────────────────────────────────── */}
            {visible('faq') && faq.body?.trim() && (
                <section className="lum-section lum-section--narrow" data-reveal>
                    <Ornament />
                    <p className="lum-eyebrow lum-eyebrow--dark">{t('wsite.good_to_know')}</p>
                    <h2 className="lum-h2 lum-h2--center">{faq.heading?.trim() || t('wsite.faq')}</h2>
                    <p className="lum-prose">{faq.body}</p>
                </section>
            )}

            {/* ── Footer ───────────────────────────────────────────── */}
            <footer className="lum-footer">
                <span className="lum-monogram">{monogram(partnerA, partnerB)}</span>
                <p className="lum-footer__names">{coupleNames}</p>
                {weddingDate && <p className="lum-footer__date">{weddingDate}</p>}
                <p className="lum-footer__brand">{t('common.powered_by')} <span>WedFlow</span></p>
            </footer>
        </div>
    );
}

/** Delicate centered gold ornament that opens each section. */
function Ornament() {
    return (
        <span className="lum-ornament" aria-hidden="true" data-reveal>
            <span className="lum-ornament__line" />
            <span className="lum-ornament__diamond" />
            <span className="lum-ornament__line" />
        </span>
    );
}

const LUMIERE_CSS = `
.lumiere {
    --paper: #FBF7F0;
    --paper-2: #F4ECDF;
    --ink: #2B2723;
    --muted: #8A7E6E;
    --gold: #B08D57;
    --gold-soft: rgba(176,141,87,0.28);
    --line: #E7DCC9;
    --serif: 'Cormorant Garamond', Georgia, serif;
    --sans: 'Jost', system-ui, sans-serif;

    background: var(--paper);
    color: var(--ink);
    font-family: var(--serif);
    min-height: 100vh;
    overflow-x: hidden;
    /* faint paper grain */
    background-image:
        radial-gradient(circle at 18% 12%, rgba(176,141,87,0.05), transparent 42%),
        radial-gradient(circle at 84% 88%, rgba(176,141,87,0.045), transparent 46%);
}

.lumiere *, .lumiere *::before, .lumiere *::after { box-sizing: border-box; }

/* ── Shared atoms ─────────────────────────────────────────── */
.lum-eyebrow {
    font-family: var(--sans);
    font-size: 11px; font-weight: 400;
    letter-spacing: 0.42em; text-transform: uppercase;
    color: rgba(255,255,255,0.85);
    display: flex; align-items: center; justify-content: center; gap: 18px;
    margin: 0 0 30px;
}
.lum-eyebrow--dark { color: var(--gold); }
.lum-rule { display: inline-block; width: 46px; height: 1px; background: currentColor; opacity: 0.6; }

.lum-h2 {
    font-family: var(--serif); font-weight: 500;
    font-size: clamp(2.1rem, 5vw, 3.4rem); line-height: 1.08;
    letter-spacing: -0.01em; margin: 0 0 24px; color: var(--ink);
}
.lum-h2--center { text-align: center; }

.lum-prose {
    font-size: clamp(1.06rem, 1.4vw, 1.28rem); line-height: 1.85;
    color: #4A4338; white-space: pre-line; margin: 0;
    font-weight: 400;
}

.lum-ornament {
    display: flex; align-items: center; justify-content: center; gap: 14px;
    margin: 0 auto 28px; width: 120px;
}
.lum-ornament__line { flex: 1; height: 1px; background: linear-gradient(90deg, transparent, var(--gold-soft)); }
.lum-ornament__line:last-child { background: linear-gradient(90deg, var(--gold-soft), transparent); }
.lum-ornament__diamond { width: 7px; height: 7px; background: var(--gold); transform: rotate(45deg); flex-shrink: 0; }

/* ── Hero ─────────────────────────────────────────────────── */
.lum-hero {
    position: relative; min-height: 100svh;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; padding: 96px 28px 72px;
    background: var(--paper-2);
}
.lum-hero--photo { background-size: cover; background-position: center; }
.lum-hero__veil {
    position: absolute; inset: 0;
    background:
        linear-gradient(180deg, rgba(20,16,12,0.42) 0%, rgba(20,16,12,0.18) 38%, rgba(20,16,12,0.55) 100%);
}
.lum-hero::before {
    /* hairline inner frame */
    content: ""; position: absolute; inset: 22px;
    border: 1px solid rgba(255,255,255,0.22); pointer-events: none;
}
.lum-hero:not(.lum-hero--photo)::before { border-color: var(--gold-soft); }
.lum-hero__inner { position: relative; z-index: 2; max-width: 880px; }

.lum-hero:not(.lum-hero--photo) .lum-eyebrow { color: var(--gold); }

.lum-names {
    font-family: var(--serif); font-weight: 500;
    color: #fff;
    font-size: clamp(3.2rem, 12vw, 7rem); line-height: 0.98;
    letter-spacing: -0.015em; margin: 0;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.lum-hero:not(.lum-hero--photo) .lum-names { color: var(--ink); }
.lum-amp {
    font-style: italic; font-weight: 400;
    color: var(--gold); font-size: 0.5em; margin: 6px 0;
}
.lum-names__a, .lum-names__b { display: block; }

.lum-meta {
    font-family: var(--sans); font-weight: 300;
    font-size: 13px; letter-spacing: 0.34em; text-transform: uppercase;
    color: rgba(255,255,255,0.9); margin: 34px 0 0;
}
.lum-hero:not(.lum-hero--photo) .lum-meta { color: var(--muted); }
.lum-venue {
    font-style: italic; font-size: 1.18rem; color: rgba(255,255,255,0.82);
    margin: 8px 0 0;
}
.lum-hero:not(.lum-hero--photo) .lum-venue { color: #5C5345; }
.lum-sub {
    font-size: 1.12rem; line-height: 1.7; color: rgba(255,255,255,0.78);
    max-width: 30rem; margin: 22px auto 0;
}
.lum-hero:not(.lum-hero--photo) .lum-sub { color: #5C5345; }

.lum-scroll {
    position: absolute; bottom: 30px; left: 50%; transform: translateX(-50%);
    z-index: 2;
    font-family: var(--sans); font-size: 9.5px; font-weight: 400;
    letter-spacing: 0.32em; text-transform: uppercase;
    color: rgba(255,255,255,0.7);
}
.lum-hero:not(.lum-hero--photo) .lum-scroll { color: var(--muted); }

/* ── Sections ─────────────────────────────────────────────── */
.lum-section {
    max-width: 1040px; margin: 0 auto;
    padding: clamp(72px, 12vw, 132px) 28px;
    border-top: 1px solid var(--line);
}
.lum-section--narrow { max-width: 720px; }
.lum-section--center { text-align: center; }
.lum-section--center .lum-ornament { margin-bottom: 28px; }

/* ── Details ──────────────────────────────────────────────── */
.lum-details {
    display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
    gap: clamp(28px, 6vw, 80px); margin-top: 44px;
}
.lum-details--single { grid-template-columns: 1fr; max-width: 26rem; margin-inline: auto; }
.lum-detail { text-align: center; }
.lum-detail__label {
    font-family: var(--sans); font-size: 11px; font-weight: 400;
    letter-spacing: 0.34em; text-transform: uppercase; color: var(--gold);
    margin: 0 0 14px;
}
.lum-detail__time { font-family: var(--serif); font-size: clamp(1.6rem, 3vw, 2.1rem); margin: 0 0 6px; color: var(--ink); }
.lum-detail__venue { font-style: italic; color: #5C5345; font-size: 1.08rem; margin: 0; }
.lum-detail__divider { width: 1px; align-self: stretch; background: linear-gradient(180deg, transparent, var(--gold-soft), transparent); }

/* ── Timeline ─────────────────────────────────────────────── */
.lum-timeline { list-style: none; margin: 44px 0 0; padding: 0; position: relative; }
.lum-timeline::before {
    content: ""; position: absolute; left: 96px; top: 6px; bottom: 6px; width: 1px;
    background: var(--line);
}
.lum-timeline__item { display: grid; grid-template-columns: 84px 24px 1fr; align-items: baseline; padding: 16px 0; }
.lum-timeline__time { font-family: var(--sans); font-size: 12px; letter-spacing: 0.12em; color: var(--gold); text-align: right; padding-top: 5px; }
.lum-timeline__node { width: 8px; height: 8px; border-radius: 50%; background: var(--paper); border: 1.5px solid var(--gold); justify-self: center; margin-top: 7px; position: relative; z-index: 1; }
.lum-timeline__body { display: flex; flex-direction: column; }
.lum-timeline__title { font-family: var(--serif); font-size: 1.4rem; color: var(--ink); }
.lum-timeline__loc { font-style: italic; color: var(--muted); font-size: 1rem; margin-top: 2px; }

/* ── Footer ───────────────────────────────────────────────── */
.lum-footer { text-align: center; padding: 80px 28px 64px; border-top: 1px solid var(--line); }
.lum-monogram {
    display: inline-grid; place-items: center; width: 58px; height: 58px;
    border: 1px solid var(--gold-soft); border-radius: 50%;
    font-family: var(--serif); font-size: 1.1rem; letter-spacing: 0.04em; color: var(--gold);
    margin-bottom: 22px;
}
.lum-footer__names { font-family: var(--serif); font-style: italic; font-size: 1.5rem; margin: 0 0 4px; color: var(--ink); }
.lum-footer__date { font-family: var(--sans); font-size: 11px; letter-spacing: 0.28em; text-transform: uppercase; color: var(--muted); margin: 0 0 26px; }
.lum-footer__brand { font-family: var(--sans); font-size: 10px; letter-spacing: 0.28em; text-transform: uppercase; color: var(--muted); margin: 0; }
.lum-footer__brand span { font-family: var(--serif); font-style: italic; text-transform: none; letter-spacing: 0; font-size: 14px; color: var(--gold); margin-left: 4px; }

/* ── Motion ───────────────────────────────────────────────── */
@keyframes lumFade { from { opacity: 0; transform: translateY(18px); } to { opacity: 1; transform: none; } }
.lum-fade { opacity: 0; animation: lumFade 1.1s cubic-bezier(0.2, 0.7, 0.2, 1) forwards; }

[data-reveal] { opacity: 0; transform: translateY(24px); transition: opacity 1s ease, transform 1s cubic-bezier(0.2,0.7,0.2,1); }
[data-reveal].is-in { opacity: 1; transform: none; }

@media (prefers-reduced-motion: reduce) {
    .lum-fade, [data-reveal] { animation: none !important; opacity: 1 !important; transform: none !important; transition: none !important; }
}

@media (max-width: 640px) {
    .lum-details { grid-template-columns: 1fr; }
    .lum-detail__divider { width: 60%; height: 1px; justify-self: center; background: linear-gradient(90deg, transparent, var(--gold-soft), transparent); }
    .lum-timeline::before { left: 70px; }
    .lum-timeline__item { grid-template-columns: 62px 20px 1fr; }
}
`;
