/* invitation-variants.jsx — two composition variations + closing confetti.
 * Composes the shared primitives from invitation-scene.jsx onto a
 * 1080x1920 canvas. Both variants share the same timeline beats (T) and
 * content (C) but differ in layout / framing.
 */
const { useTime, Easing, clamp } = window;
const { Appear, SkyBackground, Bokeh, Sparkles, Balloon, Dove, Cross, Medallion } = window;

const DURATION = 18;

const T = {
  medIn: 2.1,
  inviteIn: 3.5, inviteOut: 6.3,
  titleIn: 6.2,
  doveIn: 7.4,
  dateIn: 8.9, timeIn: 9.5, venueIn: 10.1,
  rsvpIn: 12.5,
  detailsOut: 14.5,
  closeIn: 15.1,
  confettiAt: 14.8,
};

const C = {
  invite: "You're Invited!",
  name: 'Pavle',
  titleLine: '1st Birthday & Christening',
  dateLabel: 'When',
  date: 'Sunday · 9th August 2026',
  timeLabel: 'Time',
  time: '15:00',
  venueLabel: 'Where',
  venue: 'Conty',
  venueAddr: 'Avijatičarska bb, Vršac, Serbia',
  rsvp: 'RSVP by 29th July · Anđelina or Miloš',
  closing: 'Come celebrate with us!',
};

/* ---- palette ---- */
const INK = '#4B5B53';        // soft sage-ink for body
const INK_SOFT = '#7C8A82';
const SAGE = '#8FA98C';

const goldText = {
  background: 'linear-gradient(178deg,#F6E7AE 0%,#E4C572 42%,#C39A3F 78%,#A87E2E 100%)',
  WebkitBackgroundClip: 'text', backgroundClip: 'text',
  WebkitTextFillColor: 'transparent', color: 'transparent',
  filter: 'drop-shadow(0 3px 12px rgba(201,162,75,0.45))',
  paddingInline: '0.12em',
};
// clip-safe solid gold (for short values like the time)
const goldSolid = {
  color: '#C7A047',
  textShadow: '0 2px 12px rgba(201,162,75,0.40)',
};

function Script({ text, size, style = {} }) {
  return <div style={{ fontFamily: "'Great Vibes', cursive", fontSize: size, lineHeight: 1.04, whiteSpace: 'pre', ...goldText, ...style }}>{text}</div>;
}
function Serif({ text, size, color = INK, weight = 600, ls = '0.01em', caps = false, style = {} }) {
  return (
    <div style={{
      fontFamily: "'Cormorant Garamond', serif", fontSize: size, fontWeight: weight, color,
      letterSpacing: ls, lineHeight: 1.12, textTransform: caps ? 'uppercase' : 'none', whiteSpace: 'pre', ...style,
    }}>{text}</div>
  );
}
function MiniLabel({ text }) {
  return (
    <div style={{
      fontFamily: "'Manrope', sans-serif", fontSize: 24, fontWeight: 700, letterSpacing: '0.34em',
      textTransform: 'uppercase', color: '#C9A24B', marginBottom: 6,
    }}>{text}</div>
  );
}

/* gold hairline divider with a tiny diamond */
function GoldDivider({ width = 320 }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 12, width }}>
      <div style={{ flex: 1, height: 1, background: 'linear-gradient(90deg, rgba(201,162,75,0) 0%, rgba(201,162,75,0.7) 100%)' }} />
      <div style={{ width: 7, height: 7, transform: 'rotate(45deg)', background: '#C9A24B' }} />
      <div style={{ flex: 1, height: 1, background: 'linear-gradient(90deg, rgba(201,162,75,0.7) 0%, rgba(201,162,75,0) 100%)' }} />
    </div>
  );
}

/* ---- shared details stack (centered flex column) ---- */
function DetailsStack({ gap = 26 }) {
  return (
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap, textAlign: 'center' }}>
      <Appear inAt={T.dateIn} outAt={T.detailsOut} rise={16}>
        <MiniLabel text={C.dateLabel} />
        <Serif text={C.date} size={50} />
      </Appear>
      <Appear inAt={T.timeIn} outAt={T.detailsOut} rise={16}>
        <MiniLabel text={C.timeLabel} />
        <Serif text={C.time} size={58} weight={700} style={goldSolid} />
      </Appear>
      <Appear inAt={T.venueIn} outAt={T.detailsOut} rise={16}>
        <MiniLabel text={C.venueLabel} />
        <Serif text={C.venue} size={52} weight={700} />
        <div style={{ fontFamily: "'Manrope', sans-serif", fontSize: 30, color: INK_SOFT, marginTop: 4, letterSpacing: '0.01em' }}>{C.venueAddr}</div>
      </Appear>
    </div>
  );
}

/* ---- closing confetti (gentle swirl, keeps drifting on hold) ---- */
const CONFETTI = (() => {
  const cols = ['#A8C3A0', '#E6C875', '#AFC9DE', '#F2C9C2', '#FBF4E7', '#C9A24B'];
  const arr = [];
  for (let i = 0; i < 46; i++) {
    arr.push({
      x: (i * 113 + 30) % 1060,
      start: (i * 167) % 2200,
      fall: 130 + (i % 6) * 34,
      sway: 26 + (i % 5) * 16,
      swayRate: 0.7 + (i % 4) * 0.25,
      rot: (i % 2 ? 1 : -1) * (60 + (i % 5) * 40),
      phase: (i * 0.7) % (Math.PI * 2),
      size: 12 + (i % 5) * 4,
      col: cols[i % cols.length],
      round: i % 3 === 0,
    });
  }
  return arr;
})();
function Confetti({ startAt }) {
  const t = useTime();
  const lt = t - startAt;
  if (lt < 0) return null;
  const globalIn = clamp(lt / 0.8, 0, 1);
  const span = 2300;
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden', pointerEvents: 'none' }}>
      {CONFETTI.map((c, i) => {
        const y = (((c.start + lt * c.fall) % span) + span) % span - 220;
        const x = c.x + Math.sin(lt * c.swayRate + c.phase) * c.sway;
        const rot = lt * c.rot + c.phase * 60;
        const life = clamp(Math.min((y + 220) / 160, (1920 - y) / 240), 0, 1);
        return (
          <div key={i} style={{
            position: 'absolute', left: x, top: y, width: c.size, height: c.round ? c.size : c.size * 0.55,
            background: c.col, borderRadius: c.round ? '50%' : 2,
            transform: `rotate(${rot}deg)`, opacity: 0.9 * life * globalIn,
            boxShadow: '0 1px 2px rgba(80,90,110,0.12)',
          }} />
        );
      })}
    </div>
  );
}

/* ---- closing message (shared, two lines so it never overflows) ---- */
function ClosingText({ size = 118, divider = 300 }) {
  return (
    <div style={{ textAlign: 'center' }}>
      <Script text="Come celebrate" size={size} style={{ marginBottom: -size * 0.18 }} />
      <Script text="with us!" size={size} />
      <div style={{ marginTop: 16, display: 'flex', justifyContent: 'center' }}><GoldDivider width={divider} /></div>
    </div>
  );
}
function ClosingMessage({ top, size = 118 }) {
  return (
    <Appear inAt={T.closeIn} rise={20} style={{ position: 'absolute', left: 0, right: 0, top, textAlign: 'center' }}>
      <ClosingText size={size} />
    </Appear>
  );
}

/* ============================================================
   VARIANT A — Centered Classic
   ============================================================ */
function VariantA() {
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
      <SkyBackground variant="A" />
      <Bokeh />
      <Sparkles />

      {/* rising balloons across the scene */}
      <Balloon x={150} baseY={1500} size={150} color="#A8C3A0" hi="#CFE0C9" mode="rise" rise={20} phase={0.2} sway={26} tilt={-5} />
      <Balloon x={930} baseY={1750} size={172} color="#AFC9DE" hi="#DCEAF3" mode="rise" rise={17} phase={1.4} sway={30} tilt={6} />
      <Balloon x={250} baseY={2150} size={120} color="#E6C875" hi="#F6E7AE" mode="rise" rise={23} phase={2.6} sway={22} tilt={4} />
      <Balloon x={860} baseY={2380} size={132} color="#F2C9C2" hi="#FBE3DD" mode="rise" rise={19} phase={3.7} sway={28} tilt={-6} />

      {/* balloons bobbing around Pavle */}
      <Balloon x={250} baseY={420} size={132} color="#E6C875" hi="#F6E7AE" mode="bob" phase={0.5} tilt={-10} />
      <Balloon x={835} baseY={400} size={150} color="#A8C3A0" hi="#CFE0C9" mode="bob" phase={2.1} tilt={11} />
      <Balloon x={185} baseY={760} size={116} color="#AFC9DE" hi="#DCEAF3" mode="bob" phase={3.3} tilt={-8} />
      <Balloon x={905} baseY={770} size={120} color="#F2C9C2" hi="#FBE3DD" mode="bob" phase={1.2} tilt={9} />

      {/* "You're Invited!" — above the medallion, early */}
      <Appear inAt={T.inviteIn} inDur={0.9} outAt={T.inviteOut} rise={26} style={{ position: 'absolute', left: 0, right: 0, top: 196, textAlign: 'center' }}>
        <Script text={C.invite} size={140} />
      </Appear>

      {/* Pavle medallion (center) */}
      <div style={{ position: 'absolute', left: 0, right: 0, top: 392, height: 540 }}>
        <Medallion size={540} glowFrom={T.medIn} />
      </div>

      {/* dove tucked above-right of the medallion */}
      <Appear inAt={T.doveIn} inDur={1.0} rise={10} style={{ position: 'absolute', left: 770, top: 372 }}>
        <Dove size={150} />
      </Appear>

      {/* title below medallion */}
      <Appear inAt={T.titleIn} inDur={0.9} outAt={T.closeIn} rise={22} style={{ position: 'absolute', left: 0, right: 0, top: 960, textAlign: 'center' }}>
        <Script text={C.name} size={172} style={{ marginBottom: -6 }} />
        <Serif text={C.titleLine} size={62} weight={600} ls="0.04em" caps style={{ color: SAGE }} />
      </Appear>

      {/* cross divider + details */}
      <Appear inAt={T.doveIn + 0.3} outAt={T.closeIn} rise={0} style={{ position: 'absolute', left: 0, right: 0, top: 1232, display: 'flex', justifyContent: 'center' }}>
        <Cross h={104} />
      </Appear>

      <div style={{ position: 'absolute', left: 0, right: 0, top: 1380, display: 'flex', justifyContent: 'center' }}>
        <DetailsStack gap={28} />
      </div>

      {/* RSVP */}
      <Appear inAt={T.rsvpIn} outAt={T.detailsOut} rise={14} style={{ position: 'absolute', left: 0, right: 0, top: 1748, textAlign: 'center' }}>
        <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 16 }}><GoldDivider width={300} /></div>
        <div style={{ fontFamily: "'Manrope', sans-serif", fontSize: 32, fontWeight: 600, color: INK_SOFT, letterSpacing: '0.02em' }}>{C.rsvp}</div>
      </Appear>

      {/* closing */}
      <ClosingMessage top={1240} size={124} />
      <Confetti startAt={T.confettiAt} />
    </div>
  );
}

/* ============================================================
   VARIANT B — Storybook Arch + Card
   ============================================================ */
function ArchFrame({ children }) {
  // a soft cream arched portal with a thin gold border
  return (
    <div style={{ position: 'absolute', left: 200, top: 70, width: 680, height: 856 }}>
      {/* gold rim */}
      <div style={{
        position: 'absolute', inset: -10, borderRadius: '344px 344px 44px 44px',
        background: 'linear-gradient(160deg,#EAD089,#C9A24B 55%,#A87E2E)',
        boxShadow: '0 18px 50px rgba(150,120,60,0.30)',
      }} />
      {/* inner sky window */}
      <div style={{ position: 'absolute', inset: 0, borderRadius: '338px 338px 40px 40px', overflow: 'hidden', background: 'linear-gradient(180deg,#DCE7F0,#EAF0EC 60%,#FBEFE6)' }}>
        <div style={{ position: 'absolute', inset: 0, background: 'radial-gradient(60% 50% at 50% 30%, rgba(255,247,228,0.7) 0%, rgba(255,247,228,0) 70%)' }} />
        {children}
      </div>
      {/* inner hairline */}
      <div style={{ position: 'absolute', inset: 8, borderRadius: '332px 332px 36px 36px', border: '1.5px solid rgba(255,255,255,0.55)', pointerEvents: 'none' }} />
    </div>
  );
}

function VariantB() {
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
      <SkyBackground variant="B" />
      <Bokeh />
      <Sparkles />

      {/* balloons clustered at the top corners, outside the arch */}
      <Balloon x={120} baseY={230} size={150} color="#A8C3A0" hi="#CFE0C9" mode="bob" phase={0.4} tilt={-12} />
      <Balloon x={62} baseY={500} size={120} color="#E6C875" hi="#F6E7AE" mode="bob" phase={2.0} tilt={-8} />
      <Balloon x={968} baseY={230} size={158} color="#AFC9DE" hi="#DCEAF3" mode="bob" phase={1.3} tilt={12} />
      <Balloon x={1020} baseY={520} size={120} color="#F2C9C2" hi="#FBE3DD" mode="bob" phase={3.1} tilt={9} />
      {/* a couple rising past the lower card */}
      <Balloon x={66} baseY={2000} size={120} color="#AFC9DE" hi="#DCEAF3" mode="rise" rise={18} phase={1.1} sway={22} />
      <Balloon x={1014} baseY={2250} size={132} color="#A8C3A0" hi="#CFE0C9" mode="rise" rise={20} phase={2.7} sway={26} />

      {/* arch portal holding medallion + invite + dove */}
      <ArchFrame>
        <Appear inAt={T.inviteIn} inDur={0.9} outAt={T.inviteOut} rise={20} style={{ position: 'absolute', left: 0, right: 0, top: 70, textAlign: 'center' }}>
          <Script text={C.invite} size={84} />
        </Appear>
        <div style={{ position: 'absolute', left: 0, right: 0, top: 260, height: 388 }}>
          <Medallion size={388} glowFrom={T.medIn} />
        </div>
        <Appear inAt={T.doveIn} inDur={1.0} rise={8} style={{ position: 'absolute', left: '50%', top: 132, transform: 'translateX(-50%)' }}>
          <Dove size={120} />
        </Appear>
      </ArchFrame>

      {/* lower card panel with the wording */}
      <div style={{ position: 'absolute', left: 90, top: 984, width: 900, height: 868 }}>
        <div style={{
          position: 'absolute', inset: 0, borderRadius: 40,
          background: 'linear-gradient(180deg, rgba(255,252,246,0.94), rgba(250,243,233,0.92))',
          border: '1.5px solid rgba(201,162,75,0.45)',
          boxShadow: '0 24px 60px rgba(120,110,80,0.18), inset 0 1px 0 rgba(255,255,255,0.7)',
          backdropFilter: 'blur(2px)',
        }} />

        {/* title + cross monogram */}
        <Appear inAt={T.titleIn} inDur={0.9} outAt={T.closeIn} rise={18} style={{ position: 'absolute', left: 0, right: 0, top: 44, textAlign: 'center' }}>
          <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 4 }}><Cross h={66} /></div>
          <Script text={C.name} size={134} style={{ marginBottom: -4 }} />
          <Serif text={C.titleLine} size={48} weight={600} ls="0.05em" caps style={{ color: SAGE }} />
        </Appear>

        {/* details inside the card */}
        <div style={{ position: 'absolute', left: 0, right: 0, top: 380, display: 'flex', justifyContent: 'center' }}>
          <DetailsStack gap={22} />
        </div>

        {/* rsvp at card foot */}
        <Appear inAt={T.rsvpIn} outAt={T.detailsOut} rise={12} style={{ position: 'absolute', left: 0, right: 0, bottom: 36, textAlign: 'center' }}>
          <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 12 }}><GoldDivider width={260} /></div>
          <div style={{ fontFamily: "'Manrope', sans-serif", fontSize: 30, fontWeight: 600, color: INK_SOFT }}>{C.rsvp}</div>
        </Appear>

        {/* closing message replaces card content */}
        <Appear inAt={T.closeIn} rise={18} style={{ position: 'absolute', left: 0, right: 0, top: 300, textAlign: 'center' }}>
          <ClosingText size={94} divider={260} />
        </Appear>
      </div>

      <Confetti startAt={T.confettiAt} />
    </div>
  );
}

Object.assign(window, { VariantA, VariantB, DURATION, INVITE_T: T });
