/* global React, INK, CREAM, PULSE, GENS, GradText, CTAButton, Reveal, useReveal, SB_EASE, UGCStatus */
// UGC Campaigns — Materials · Posting · Analytics · Payouts · How it runs.
const { useState: ugS } = React;

const UGCBadge = ({ label, ac = '#813CED', bg = CREAM }) =>
  <div style={{ display: 'inline-flex', alignItems: 'center', gap: 8, marginBottom: 18, borderRadius: 9999, padding: '7px 14px', background: bg, boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.1)' }}>
    <span style={{ width: 7, height: 7, borderRadius: '50%', background: ac }} />
    <span style={{ fontSize: 11.5, fontWeight: 700, color: INK }}>{label}</span>
  </div>;

const UGCShell = ({ label, ac, h1, h2, sub, note, panel, flip = false, lean = 'right', bg = '#fff' }) => (
  <section style={{ background: bg, padding: '110px 56px' }}>
    <div style={{ maxWidth: 1140, margin: '0 auto', display: 'grid', gridTemplateColumns: flip ? '1.1fr 0.9fr' : '0.9fr 1.1fr', gap: 64, alignItems: 'center' }}>
      <div style={{ order: flip ? 2 : 1 }}>
        <Reveal delay={90}>
          <h2 style={{ font: '800 52px/1 var(--font-sans)', letterSpacing: '-0.03em', color: INK, margin: '0 0 18px', textWrap: 'balance' }}>{h1}<br /><GradText>{h2}</GradText></h2>
        </Reveal>
        <Reveal delay={170}><p style={{ fontSize: 17, lineHeight: 1.55, color: 'var(--text-secondary)', margin: '0 0 28px', maxWidth: 420 }}>{sub}</p></Reveal>
        <Reveal delay={250}><CTAButton>Launch a campaign</CTAButton></Reveal>
        {note && <Reveal delay={330}><div style={{ marginTop: 26, fontSize: 13, color: 'var(--text-tertiary)' }}>{note}</div></Reveal>}
      </div>
      <Reveal delay={160} y={28} style={{ order: flip ? 1 : 2 }}>
        <div style={{ transform: lean === 'left' ? 'rotate(-1.3deg) perspective(1400px) rotateY(4deg)' : 'rotate(1.3deg) perspective(1400px) rotateY(-4deg)', transformOrigin: 'center' }}>{panel}</div>
      </Reveal>
    </div>
  </section>
);

const UGCCard = ({ children, pad = 18 }) =>
  <div style={{ borderRadius: 20, background: '#fff', boxShadow: '0 24px 48px rgba(28,27,27,0.14), inset 0 0 0 1px rgba(28,27,27,0.08)', padding: pad }}>{children}</div>;

// ---------- § 01 Materials ----------
const UGC_OBJECTS = [['assets/control/collar-silver.png', 'Sculpt Collar'], ['assets/control/heels-black.webp', 'Aurelle Heel'], ['assets/control/top-red.webp', 'Slip Top']];
const UGC_OUTFITS = [['assets/garments/top-velvet-emerald.webp', 'Velvet Halter'], ['assets/garments/bottom-mini-oxblood.webp', 'Oxblood Mini'], ['assets/garments/shoe-stiletto-gold.webp', 'Gold Stiletto']];

const MaterialsPanel = () => {
  const [ref, on] = useReveal(0.35);
  const Lib = ({ title, items, d0 }) => (
    <div>
      <div style={{ fontSize: 10, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-tertiary)', marginBottom: 8 }}>{title}</div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 7 }}>
        {items.map(([src, n], i) =>
          <div key={n} style={{ borderRadius: 11, background: 'rgb(250,247,247)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.08)', padding: '8px 6px 7px', textAlign: 'center',
            opacity: on ? 1 : 0, transform: on ? 'none' : 'translateY(8px)', transition: `all 0.45s ${SB_EASE} ${d0 + i * 80}ms` }}>
            <img loading="lazy" decoding="async" src={src} alt={n} style={{ width: '100%', height: 52, objectFit: 'contain', marginBottom: 5 }} />
            <div style={{ fontSize: 8.5, fontWeight: 700, color: INK, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{n}</div>
          </div>)}
      </div>
    </div>);
  return (
    <UGCCard>
      <div ref={ref}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 13 }}>
          <span style={{ fontSize: 12.5, fontWeight: 700, color: INK }}>Aurelle brand kit</span>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 9.5, fontWeight: 700, color: 'var(--brand-purple-700)', background: 'var(--brand-purple-50)', border: '1px solid var(--brand-purple-200)', borderRadius: 9999, padding: '3px 10px' }}>
            <span style={{ width: 5, height: 5, borderRadius: '50%', background: 'var(--brand-purple-500)' }} />Synced to 12 creator studios</span>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 13, marginBottom: 13 }}>
          <Lib title="Object library — your products" items={UGC_OBJECTS} d0={100} />
          <Lib title="Outfit library — the campaign wardrobe" items={UGC_OUTFITS} d0={340} />
        </div>
        <div style={{ display: 'flex', gap: 5, flexWrap: 'wrap' }}>
          {['No samples', 'No shipping', 'No returns', 'Pixel-consistent'].map(c =>
            <span key={c} style={{ borderRadius: 9999, padding: '5px 11px', fontSize: 10, fontWeight: 600, background: 'rgb(246,243,242)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.08)', color: 'rgba(28,27,27,0.65)' }}>✓ {c}</span>)}
        </div>
      </div>
    </UGCCard>);
};

// ---------- § 02 Posting ----------
const UGC_STAGES = ['Brief', 'Created', 'Approved', 'Posted'];
const UGC_TRACK = [
  { g: GENS[0], n: 'Scarlett', p: 'IG Reel', done: 4, s: 'posted', note: 'Verified 9:02 PM' },
  { g: GENS[5], n: 'Nova', p: 'TikTok', done: 3, s: 'scheduled', note: 'Publishes Fri' },
  { g: GENS[3], n: 'Jade', p: 'TikTok', done: 1, s: 'nudged', note: 'Due in 2 days' }];

const TrackerPanel = () => {
  const [ref, on] = useReveal(0.35);
  return (
    <UGCCard>
      <div ref={ref}>
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 12 }}>
          <span style={{ fontSize: 12.5, fontWeight: 700, color: INK }}>Deliverables</span>
          <span style={{ fontSize: 10.5, fontWeight: 600, color: 'var(--text-tertiary)' }}>Brief → Created → Approved → Posted</span>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
          {UGC_TRACK.map((r, i) =>
            <div key={r.n} style={{ borderRadius: 12, background: 'rgb(250,247,247)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.08)', padding: '10px 12px',
              opacity: on ? 1 : 0, transition: `opacity 0.45s ${SB_EASE} ${i * 110}ms` }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
                <span style={{ width: 26, height: 26, borderRadius: '50%', overflow: 'hidden', flexShrink: 0 }}>
                  <img loading="lazy" decoding="async" src={r.g.src} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center 14%' }} /></span>
                <span style={{ fontSize: 11.5, fontWeight: 700, color: INK, flex: 1 }}>{r.n} <span style={{ fontSize: 9.5, fontWeight: 600, color: 'rgba(28,27,27,0.45)' }}>· {r.p}</span></span>
                <UGCStatus s={r.s} />
              </div>
              <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                {UGC_STAGES.map((st, k) => (
                  <React.Fragment key={st}>
                    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, flexShrink: 0 }}>
                      <span style={{ width: 14, height: 14, borderRadius: '50%', display: 'grid', placeItems: 'center', fontSize: 8, fontWeight: 800,
                        background: k < r.done ? PULSE : '#fff', color: k < r.done ? '#fff' : 'rgba(28,27,27,0.35)',
                        boxShadow: k < r.done ? 'none' : 'inset 0 0 0 1px rgba(28,27,27,0.18)' }}>{k < r.done ? '✓' : ''}</span>
                      <span style={{ fontSize: 8.5, fontWeight: 700, color: k < r.done ? INK : 'rgba(28,27,27,0.4)' }}>{st}</span>
                    </span>
                    {k < 3 && <span style={{ flex: 1, height: 1.5, borderRadius: 2, background: k < r.done - 1 ? 'var(--brand-purple-400)' : 'rgba(28,27,27,0.12)' }} />}
                  </React.Fragment>))}
              </div>
              <div style={{ marginTop: 7, fontSize: 9.5, fontWeight: 600, color: r.s === 'nudged' ? '#B45309' : 'rgba(28,27,27,0.5)' }}>
                {r.s === 'nudged' ? '⚡ Auto-nudge sent — deadline reminder, no DM from you' : r.note}</div>
            </div>)}
        </div>
      </div>
    </UGCCard>);
};

// ---------- § 03 Analytics ----------
const UGC_BARS = [['Scarlett', 100, '412k', true], ['Nova', 70, '287k', true], ['Mara', 48, '198k', true], ['Jade', 0, '—', false]];
const UGCAnalyticsPanel = () => {
  const [ref, on] = useReveal(0.35);
  const [tab, setTab] = ugS(0);
  return (
    <UGCCard>
      <div ref={ref}>
        <div style={{ display: 'flex', gap: 5, marginBottom: 13 }}>
          {['By creator', 'By post', 'By platform'].map((p, i) =>
            <span key={p} onClick={() => setTab(i)} style={{ borderRadius: 9999, padding: '6px 12px', fontSize: 10.5, fontWeight: tab === i ? 700 : 600, cursor: 'pointer', whiteSpace: 'nowrap', transition: 'all 0.18s',
              background: tab === i ? INK : 'rgb(246,243,242)', color: tab === i ? '#fff' : 'rgba(28,27,27,0.6)',
              boxShadow: tab === i ? 'none' : 'inset 0 0 0 1px rgba(28,27,27,0.08)' }}>{p}</span>)}
          <span style={{ marginLeft: 'auto', fontSize: 10.5, fontWeight: 600, color: 'var(--text-tertiary)', alignSelf: 'center' }}>Campaign to date</span>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 8, marginBottom: 13 }}>
          {[['Views', '2.1M', '▲ 22%'], ['Engagement', '8.4%', '▲ 1.1'], ['Link taps', '31k', '▲ 9%']].map(([l, v, d]) =>
            <div key={l} style={{ borderRadius: 12, background: 'rgb(250,247,247)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.07)', padding: '10px 12px' }}>
              <div style={{ fontSize: 8.5, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-tertiary)', marginBottom: 4 }}>{l}</div>
              <div style={{ fontSize: 16, fontWeight: 800, letterSpacing: '-0.02em', color: INK }}>{v} <span style={{ fontSize: 9, fontWeight: 700, color: '#15803D' }}>{d}</span></div>
            </div>)}
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
          {UGC_BARS.map(([n, w, v, ok], i) =>
            <div key={n} style={{ display: 'grid', gridTemplateColumns: '72px 1fr 84px', alignItems: 'center', gap: 10 }}>
              <span style={{ fontSize: 10.5, fontWeight: 600, color: 'var(--text-secondary)' }}>{n}</span>
              <span style={{ height: 18, borderRadius: 6, background: 'rgb(240,236,235)', overflow: 'hidden' }}>
                <span style={{ display: 'block', height: '100%', borderRadius: 6, background: PULSE, width: on ? `${(tab === 1 ? 0.8 : tab === 2 ? 0.6 : 1) * w}%` : '2%', transition: `width 0.8s ${SB_EASE} ${i * 100}ms` }} /></span>
              <span style={{ fontSize: 9.5, fontWeight: 700, color: INK, textAlign: 'right', fontVariantNumeric: 'tabular-nums' }}>{v} {ok && <span style={{ color: '#15803D' }}>✓</span>}</span>
            </div>)}
        </div>
        <div style={{ marginTop: 12, fontSize: 11, color: 'var(--text-secondary)' }}>✓ = read from the publishing pipeline, not self-reported. Jade hasn’t posted — so she has no numbers to inflate.</div>
      </div>
    </UGCCard>);
};

// ---------- § 04 Payouts ----------
const UGC_PAY = [
  { g: GENS[0], n: 'Scarlett', a: '$400', s: 'Paid', d: 'Released on verified post · 9:02 PM' },
  { g: GENS[5], n: 'Nova', a: '$400', s: 'Holds', d: 'Releases Friday, when the post verifies' },
  { g: GENS[3], n: 'Jade', a: '$400', s: 'Escrow', d: 'Returns to you if the deadline passes' }];

const PayoutsPanel = () => {
  const [ref, on] = useReveal(0.35);
  return (
    <UGCCard>
      <div ref={ref}>
        <div style={{ borderRadius: 13, background: 'rgb(250,247,247)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.07)', padding: '11px 13px', marginBottom: 12 }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 10, marginBottom: 7 }}>
            <span style={{ fontWeight: 700, color: INK }}>$4,800 escrowed · one charge</span>
            <span style={{ color: 'rgba(28,27,27,0.5)' }}>$1,600 released · $3,200 held</span>
          </div>
          <div style={{ height: 9, borderRadius: 9999, background: 'rgb(235,231,231)', overflow: 'hidden', display: 'flex' }}>
            <span style={{ width: on ? '33%' : '4%', height: '100%', background: PULSE, transition: `width 0.9s ${SB_EASE} 250ms` }} />
            <span style={{ width: on ? '11%' : '0%', height: '100%', background: 'var(--brand-purple-200)', transition: `width 0.9s ${SB_EASE} 400ms` }} />
          </div>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 7, marginBottom: 12 }}>
          {UGC_PAY.map((r, i) =>
            <div key={r.n} style={{ display: 'flex', alignItems: 'center', gap: 9, borderRadius: 11, background: '#fff', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.1)', padding: '8px 11px',
              opacity: on ? 1 : 0, transition: `opacity 0.45s ${SB_EASE} ${150 + i * 100}ms` }}>
              <span style={{ width: 26, height: 26, borderRadius: '50%', overflow: 'hidden', flexShrink: 0 }}>
                <img loading="lazy" decoding="async" src={r.g.src} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center 14%' }} /></span>
              <span style={{ flex: 1, minWidth: 0 }}>
                <span style={{ display: 'block', fontSize: 11, fontWeight: 700, color: INK }}>{r.n} <span style={{ fontWeight: 800 }}>· {r.a}</span></span>
                <span style={{ display: 'block', fontSize: 9, fontWeight: 600, color: 'rgba(28,27,27,0.5)' }}>{r.d}</span>
              </span>
              <span style={{ fontSize: 9, fontWeight: 700, borderRadius: 9999, padding: '3px 9px', flexShrink: 0,
                color: r.s === 'Paid' ? '#15803D' : r.s === 'Holds' ? '#4D73F2' : 'rgba(28,27,27,0.55)',
                background: r.s === 'Paid' ? 'rgba(34,197,94,0.12)' : r.s === 'Holds' ? 'rgba(77,115,242,0.1)' : 'rgb(246,243,242)',
                border: `1px solid ${r.s === 'Paid' ? 'rgba(34,197,94,0.3)' : r.s === 'Holds' ? 'rgba(77,115,242,0.3)' : 'rgba(28,27,27,0.12)'}` }}>{r.s}</span>
            </div>)}
        </div>
        <div style={{ fontSize: 11, color: 'var(--text-secondary)' }}>One receipt from Sozee — not twelve invoices, twelve wires and a tax-form folder.</div>
      </div>
    </UGCCard>);
};

// ---------- Sections ----------
const MaterialsSection = () => <UGCShell label="Materials" ac="#C77D3A" h1="Ship pixels," h2="not packages." lean="left"
  sub="Upload the campaign kit once — products to the object library, the wardrobe to the outfit library. It lands in every accepted creator’s studio the same minute, and every generation renders it pixel-consistent."
  panel={<MaterialsPanel />} />;
const TrackerSection = () => <UGCShell label="Posting" ac="#4D73F2" h1="They post." h2="You’ll know." flip lean="right" bg={CREAM}
  sub="Every deliverable is tracked from brief to publish. Posts go out through Sozee’s scheduler, so “posted” is verified — and creators drifting toward a deadline get nudged automatically, not chased by you."
  panel={<TrackerPanel />} />;
const UGCAnalyticsSection = () => <UGCShell label="Analytics" ac="#813CED" h1="Numbers from" h2="the source." lean="left"
  sub="Reporting comes from the same pipeline that publishes — views, engagement and link taps per creator, per post, per platform. Nothing self-reported, nothing screenshotted."
  panel={<UGCAnalyticsPanel />} />;
const PayoutsSection = () => <UGCShell label="Payouts" ac="#3A8F6B" h1="Escrow in." h2="Receipts out." flip lean="right" bg={CREAM}
  sub="Fund the campaign once. Each creator is paid automatically the moment their post verifies — miss the deadline and that slice returns to you. No invoices, no wires, no chasing."
  panel={<PayoutsPanel />} />;

// ---------- § 05 How it runs ----------
const UGC_STEPS = [
  ['01', 'Brief it', 'Deliverables, deadline, rate per creator — one page.'],
  ['02', 'Stock the kit', 'Products and wardrobe sync to every accepted studio.'],
  ['03', 'They create & post', 'Content is approved, scheduled and verified on publish.'],
  ['04', 'Payouts release', 'Escrow pays on proof. You read one dashboard.']];

const HowItRunsSection = () => (
  <section style={{ background: '#fff', padding: '110px 56px' }}>
    <div style={{ maxWidth: 1140, margin: '0 auto', textAlign: 'center' }}>
      <Reveal delay={90}>
        <h2 style={{ font: '800 52px/1 var(--font-sans)', letterSpacing: '-0.03em', color: INK, margin: '0 0 18px' }}>
          Live in<br /><GradText>four moves.</GradText>
        </h2>
      </Reveal>
      <Reveal delay={170}>
        <p style={{ fontSize: 17, lineHeight: 1.55, color: 'var(--text-secondary)', margin: '0 auto 46px', maxWidth: 480 }}>
          The whole campaign runs inside one board — the part of the job that used to be spreadsheets, DMs and follow-ups.
        </p>
      </Reveal>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 16 }}>
        {UGC_STEPS.map(([n, t, d], i) =>
          <Reveal key={n} delay={i * 100} y={22}>
            <div style={{ borderRadius: 16, background: CREAM, boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.08)', padding: '22px 18px', textAlign: 'left', height: '100%', boxSizing: 'border-box',
              transform: `rotate(${i % 2 ? 0.8 : -0.8}deg)` }}>
              <div style={{ fontSize: 11, fontWeight: 800, letterSpacing: '0.1em', color: 'var(--brand-purple-700)', marginBottom: 10 }}>§ {n}</div>
              <div style={{ fontSize: 16.5, fontWeight: 700, letterSpacing: '-0.01em', color: INK, marginBottom: 6 }}>{t}</div>
              <div style={{ fontSize: 12.5, lineHeight: 1.5, color: 'var(--text-secondary)' }}>{d}</div>
            </div>
          </Reveal>)}
      </div>
    </div>
  </section>
);

Object.assign(window, { MaterialsSection, TrackerSection, UGCAnalyticsSection, PayoutsSection, HowItRunsSection });
