/* Static content screens: #privacy, #help, #refunds.
   Built on the Sparkle Classic design system + shared chrome (SiteFooter).
   Copy is grounded in how the product actually works (see api/* + intake.jsx).
   Reached via the footer links and the intake consent "Privacy policy" link. */
const DSp = window.SparkleClassicDesignSystem_7a1470;
const SUPPORT_EMAIL = 'support@sparkleclassic.com';
const UPDATED = 'June 15, 2026';

/* one-time CSS for the FAQ disclosure + page rhythm */
function PageStyles() {
  return (
    <style dangerouslySetInnerHTML={{ __html: `
      .spk-page { padding: 0 0 8px; }
      .spk-page__band { padding: 26px 18px 22px; text-align: center;
        box-shadow: 0 3px 0 0 var(--plum-ink), 0 -3px 0 0 var(--plum-ink); }
      .spk-page__glyph { width: 56px; height: 56px; display: inline-flex; align-items: center;
        justify-content: center; font-size: 28px; background: var(--cream);
        box-shadow: var(--ofx-edges); margin-bottom: 10px; }
      .spk-page__body { padding: 22px 18px 8px; display: flex; flex-direction: column; gap: 14px; }
      .spk-prose p { margin: 0 0 9px; font-size: 14.5px; line-height: 1.55; color: var(--plum-ink); }
      .spk-prose p:last-child { margin-bottom: 0; }
      .spk-prose strong { color: var(--plum-ink); }
      .spk-prose a { color: var(--plum-accent); font-weight: 700; }
      .spk-faq { list-style: none; }
      .spk-faq summary { list-style: none; cursor: pointer; display: flex; align-items: center;
        gap: 10px; font-weight: 800; font-size: 15px; color: var(--plum-ink); padding: 2px 0; }
      .spk-faq summary::-webkit-details-marker { display: none; }
      .spk-faq summary .spk-faq__tw { margin-left: auto; font-family: var(--font-pixel); font-size: 12px;
        color: var(--plum-accent); transition: transform .15s ease; }
      .spk-faq[open] summary .spk-faq__tw { transform: rotate(45deg); }
      .spk-faq__a { margin: 9px 0 2px; font-size: 14px; line-height: 1.55; color: var(--plum-ink); }
      .spk-faq__a a { color: var(--plum-accent); font-weight: 700; }
    ` }} />
  );
}

function PageShell({ glyph, band = 'var(--cotton-blue)', eyebrow, title, lead, onHome, onStart, children }) {
  const { Button, Icon } = DSp;
  return (
    <div className="spk-page">
      <PageStyles />
      <section className="spk-page__band" style={{ background: band }}>
        <span className="spk-page__glyph" aria-hidden="true">{glyph}</span>
        <p className="spk-eyebrow" style={{ margin: '0 0 4px' }}>{eyebrow}</p>
        <h1 className="spk-h2" style={{ fontSize: 26, margin: '0 auto', maxWidth: 340 }}>{title}</h1>
        {lead && <p className="spk-muted" style={{ margin: '8px auto 0', maxWidth: 360, fontSize: 14 }}>{lead}</p>}
      </section>
      <div className="spk-page__body">{children}</div>
      <div style={{ display: 'flex', justifyContent: 'center', padding: '8px 18px 22px' }}>
        <Button variant="ghost" onClick={onHome} leftIcon={<Icon name="arrow-left" />}>Back to store</Button>
      </div>
      <SiteFooter onStart={onStart} />
    </div>
  );
}

function InfoCard({ title, children }) {
  const { Card } = DSp;
  return (
    <Card surface="card" padding="md">
      {title && <h2 className="spk-h4" style={{ fontSize: 17, margin: '0 0 8px' }}>{title}</h2>}
      <div className="spk-prose">{children}</div>
    </Card>
  );
}

function Faq({ q, children, open = false }) {
  return (
    <details className="spk-faq" open={open}>
      <summary>{q}<span className="spk-faq__tw" aria-hidden="true">+</span></summary>
      <div className="spk-faq__a">{children}</div>
    </details>
  );
}

function MailLink() { return <a href={`mailto:${SUPPORT_EMAIL}`}>{SUPPORT_EMAIL}</a>; }

/* Support contact form → POST /api/wall { action:'support' } → AgentMail to the
   team (reply-to the customer) + a confirmation to the customer. */
const SUBJECTS = ['A question about my order', 'I need help with my game',
  'Photo / sprite question', 'Refund request', 'Press / partnership', 'Something else'];
function ContactForm() {
  const { Card, Input, Select, Textarea, Button, Icon } = DSp;
  const [f, setF] = React.useState({ name: '', email: '', subject: SUBJECTS[0], message: '' });
  const [state, setState] = React.useState('idle'); // idle | sending | sent | error
  const [err, setErr] = React.useState(null);
  const set = (k, v) => setF((p) => ({ ...p, [k]: v }));
  const valid = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(f.email.trim()) && f.message.trim().length > 1;

  const submit = async () => {
    if (state === 'sending') return;
    if (!valid) { setErr('Please add your email and a short message.'); return; }
    setState('sending'); setErr(null);
    try {
      const r = await fetch('/api/wall', {
        method: 'POST', headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ action: 'support', name: f.name.trim(), email: f.email.trim(), subject: f.subject, message: f.message.trim() }),
      });
      const out = await r.json().catch(() => ({}));
      if (!r.ok || !out.ok) throw new Error(out.error || 'Something went wrong. Please try again.');
      setState('sent');
    } catch (e) { setState('error'); setErr((e && e.message) || 'Something went wrong. Please try again.'); }
  };

  if (state === 'sent') {
    return (
      <Card surface="cream" padding="lg" style={{ textAlign: 'center' }}>
        <div style={{ fontSize: 36 }} aria-hidden="true">💌</div>
        <h2 className="spk-h3" style={{ fontSize: 20, margin: '6px 0 6px' }}>Message sent!</h2>
        <p style={{ margin: 0, fontSize: 14.5, color: 'var(--plum-ink)' }}>
          A real person will reply to <strong>{f.email}</strong> within about a day. Check your inbox
          for our confirmation. 💛
        </p>
      </Card>
    );
  }

  return (
    <Card surface="card" padding="md">
      <h2 className="spk-h4" style={{ fontSize: 18, margin: '0 0 4px' }}>Send us a message</h2>
      <p className="spk-muted" style={{ margin: '0 0 14px', fontSize: 13.5 }}>
        A real person reads every one — usually a reply within a day.
      </p>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
        <Input label="Your email" type="email" inputMode="email" placeholder="you@email.com"
          leftIcon={<Icon name="mail" />} value={f.email} onChange={(e) => set('email', e.target.value)} required />
        <Input label="Your name" placeholder="e.g. Dana" value={f.name} onChange={(e) => set('name', e.target.value)} />
        <Select label="What's this about?" value={f.subject} options={SUBJECTS}
          onChange={(e) => set('subject', e.target.value)} />
        <Textarea label="How can we help?" rows={4} maxLength={2000} placeholder="Tell us what's up…"
          value={f.message} onChange={(e) => set('message', e.target.value)} required />
      </div>
      {err && (
        <p className="spk-field__error" role="alert" style={{ display: 'flex', alignItems: 'center', gap: 6, marginTop: 10, color: 'var(--danger)', fontSize: 13, fontWeight: 700 }}>
          <Icon name="alert" size={15} aria-hidden="true" /> {err}
        </p>
      )}
      <Button variant="primary" fullWidth onClick={submit} disabled={state === 'sending'}
        rightIcon={state === 'sending' ? null : <Icon name="arrow-right" />} style={{ marginTop: 14 }}>
        {state === 'sending' ? 'Sending…' : 'Send message'}
      </Button>
      <p className="spk-muted" style={{ textAlign: 'center', fontSize: 12.5, marginTop: 10 }}>
        Prefer email? <MailLink />
      </p>
    </Card>
  );
}

/* ===================== PRIVACY ===================== */
function PrivacyScreen({ onHome, onStart }) {
  return (
    <PageShell glyph="🔒" band="var(--cotton-blue)" eyebrow="PRIVACY"
      title="Your photos are for one thing only" onHome={onHome} onStart={onStart}
      lead={`Plain-English version. Last updated ${UPDATED}.`}>
      <InfoCard title="What we collect">
        <p>Only what we need to make and send your pet&rsquo;s game:</p>
        <p>&bull; Your email (so we can send your game link)<br />
          &bull; Your name (optional)<br />
          &bull; Your pet&rsquo;s name<br />
          &bull; One photo of your pet (and, if you add the toy, one photo of the toy)<br />
          &bull; Anything you type in the notes box</p>
        <p>That&rsquo;s it. No tracking pixels, no ad profiles.</p>
      </InfoCard>

      <InfoCard title="Your photo — and when we delete it">
        <p>Your photo is used for <strong>one purpose</strong>: so a real person can hand-draw your
          pet&rsquo;s pixel sprite. We never use it for anything else.</p>
        <p>It&rsquo;s deleted automatically <strong>within about 48 hours after your game is delivered</strong>.
          If you start an order but don&rsquo;t finish paying, that photo is deleted within 7 days.</p>
        <p>The finished pixel sprite (your actual game) is kept so your private link keeps working.
          Your photo is also shrunk in your own browser <em>before</em> it&rsquo;s ever uploaded.</p>
      </InfoCard>

      <InfoCard title="Payment">
        <p>Payments are handled securely by <strong>Stripe</strong>. We never see or store your full
          card number.</p>
      </InfoCard>

      <InfoCard title="Who helps us run Sparkle Classic">
        <p>A few trusted services, each getting only what it needs:</p>
        <p>&bull; <strong>Stripe</strong> — payments &amp; refunds<br />
          &bull; <strong>AgentMail</strong> — sends your game link and our support replies<br />
          &bull; <strong>Turso</strong> — our order database<br />
          &bull; <strong>Vercel</strong> — hosting and the daily privacy clean-up</p>
      </InfoCard>

      <InfoCard title="Your private game link">
        <p>Your game lives at <strong>sparkleclassic.com/yourpet</strong>. It&rsquo;s unlisted — only
          people you share the link with can find it.</p>
      </InfoCard>

      <InfoCard title="Your choices">
        <p>Want your data or your game removed? Email <MailLink /> and we&rsquo;ll take care of it.</p>
        <p>We don&rsquo;t sell your data — ever — and we don&rsquo;t use your pet&rsquo;s photo for anything
          but making your sprite. Questions about privacy? Same address: <MailLink />.</p>
      </InfoCard>
    </PageShell>
  );
}

/* ===================== HELP / FAQ ===================== */
function HelpScreen({ onHome, onStart }) {
  const { Card, Button, Icon, Sparkle } = DSp;
  return (
    <PageShell glyph="💬" band="var(--pink-soft)" eyebrow="HELP"
      title="Questions? We've got you" onHome={onHome} onStart={onStart}
      lead="How Sparkle Classic works, start to finish.">
      <Card surface="card" padding="md">
        <Faq q="How does it work?" open>
          Four steps: <strong>send one photo of your pet</strong> and their name, <strong>pay $29</strong>,
          a human <strong>recreates your pet into a pixelated game sprite</strong>, and within about
          24&nbsp;hours we email you a <strong>private link</strong> to play their very own game.
        </Faq>
        <Faq q="How long does it take?">
          About <strong>24 hours</strong>. A real person draws every sprite by hand, so it isn&rsquo;t
          instant — but it&rsquo;s usually ready within a day. If it ever takes longer, your money-back
          promise still stands.
        </Faq>
        <Faq q="What photo works best?">
          One <strong>clear, well-lit</strong> photo of your pet facing the camera — one pet per game.
          JPG, PNG, or HEIC all work. Don&rsquo;t worry about file size; your browser shrinks it before
          it uploads.
        </Faq>
        <Faq q="What do I actually get?">
          A real, playable <strong>retro platformer starring your pet</strong>, at your own private link
          (<strong>sparkleclassic.com/yourpet</strong>). Play it on your phone or computer and
          share it with anyone you like.
        </Faq>
        <Faq q="Can I add my pet's favorite toy?">
          Yes! Add their toy for <strong>$5</strong> at checkout and we&rsquo;ll pixel-ify it and hide it
          in the game for your pet to collect.
        </Faq>
        <Faq q="Does it work for cats and dogs?">
          Both! Sparkle Classic makes a game for cats <em>and</em> dogs. If they&rsquo;re your best friend,
          they can be the hero.
        </Faq>
        <Faq q="Is it a good gift?">
          It&rsquo;s our favorite reason to make one. A one-of-a-kind game starring someone&rsquo;s best
          friend is the kind of gift people remember.
        </Faq>
        <Faq q="What if my photo doesn't work?">
          If we can&rsquo;t get a great sprite from your photo, we&rsquo;ll email you to ask for another —
          no extra charge, and the clock pauses until we have what we need.
        </Faq>
        <Faq q="Can I change my pet's name or link?">
          Your link comes from your pet&rsquo;s name. Email <MailLink /> before we finish and we&rsquo;ll
          adjust it for you.
        </Faq>
        <Faq q="What if I'm not happy?">
          You&rsquo;re covered by our money-back promise — see the <a href="#refunds">Refunds</a> page.
        </Faq>
        <Faq q="I lost my game link!">
          Check the email we sent when your game was ready, or email <MailLink /> with your pet&rsquo;s name
          and we&rsquo;ll resend it.
        </Faq>
      </Card>

      <div style={{ textAlign: 'center', paddingTop: 4 }}>
        <Sparkle size={24} twinkle />
        <h2 className="spk-h3" style={{ fontSize: 20, margin: '4px 0 0' }}>Still have a question?</h2>
      </div>
      <ContactForm />

      <Card surface="pink" padding="lg" style={{ textAlign: 'center' }}>
        <p style={{ margin: '0 auto 14px', maxWidth: 300, fontSize: 15, color: 'var(--plum-ink)', fontWeight: 700 }}>
          Ready to make their game?
        </p>
        <Button variant="primary" size="lg" fullWidth onClick={onStart}
          rightIcon={<Icon name="arrow-right" />} style={{ maxWidth: 360, margin: '0 auto' }}>
          Make my pet a game · $29
        </Button>
      </Card>
    </PageShell>
  );
}

/* ===================== REFUNDS ===================== */
function RefundsScreen({ onHome, onStart }) {
  const { Button, Icon } = DSp;
  return (
    <PageShell glyph="💛" band="var(--mint)" eyebrow="REFUNDS"
      title="Love it, or we'll make it right" onHome={onHome} onStart={onStart}
      lead={`Fair both ways. Last updated ${UPDATED}.`}>
      <InfoCard title="We stand behind every game">
        <p>Every Sparkle Classic game is made by hand for <strong>your</strong> pet, and we want you to
          love it. If something&rsquo;s wrong, we&rsquo;ll fix it — and if we genuinely can&rsquo;t make it
          right, you get your money back. Here&rsquo;s exactly how.</p>
      </InfoCard>

      <InfoCard title="Changed your mind? (before we start)">
        <p>If you cancel <strong>before a human starts</strong> drawing your pet, you get a
          <strong> full refund</strong>, no questions asked. Just email <MailLink /> with your pet&rsquo;s
          name and we&rsquo;ll sort it.</p>
      </InfoCard>

      <InfoCard title="Something not right? We'll fix it free">
        <p>If your sprite doesn&rsquo;t look like your pet, the game won&rsquo;t load, or we got something
          wrong — tell us within <strong>14 days</strong> and we&rsquo;ll <strong>redo it free</strong>.
          If we still can&rsquo;t make it right, you get a <strong>full refund</strong>. That&rsquo;s our
          real guarantee: we deliver a pet game you&rsquo;re happy with, or you don&rsquo;t pay.</p>
      </InfoCard>

      <InfoCard title="Made just for your pet">
        <p>Because every game is one-of-a-kind, hand-made art of your specific pet, we can&rsquo;t offer
          refunds for a simple <strong>change of heart after it&rsquo;s delivered</strong> — the work is
          done and the game is yours to keep. The &ldquo;we&rsquo;ll fix it&rdquo; guarantee above always
          applies, though.</p>
      </InfoCard>

      <InfoCard title="If we're running late">
        <p>We aim for about 24 hours. If there&rsquo;s <strong>no game after 3 days</strong>, that&rsquo;s
          on us — full refund, or keep waiting, your call.</p>
      </InfoCard>

      <InfoCard title="How to ask">
        <p>From your order page tap <strong>&ldquo;Request a refund&rdquo;</strong>, or email <MailLink />
          with your pet&rsquo;s name and what went wrong.</p>
        <p>A real person reviews every request within about a day — we don&rsquo;t auto-approve, so we can
          actually help (most of the time a quick fix is all it takes).</p>
      </InfoCard>

      <InfoCard title="The little print">
        <p>&bull; Your $29 is held safely and is only fully ours once your game is made.<br />
          &bull; Approved refunds go to your original payment method via Stripe — about <strong>5 to 10
          days</strong> on your statement.<br />
          &bull; If you added the $5 toy, it&rsquo;s included in any refund.<br />
          &bull; One game per order. This doesn&rsquo;t affect any rights you have under your local
          consumer law.</p>
      </InfoCard>

      <div style={{ display: 'flex', justifyContent: 'center', paddingTop: 2 }}>
        <Button variant="primary" onClick={onStart} rightIcon={<Icon name="arrow-right" />} style={{ maxWidth: 360 }}>
          Make my pet a game · $29
        </Button>
      </div>
    </PageShell>
  );
}

Object.assign(window, { PrivacyScreen, HelpScreen, RefundsScreen });
