/* global React, Icon, FaqItem */

// Every topic is rendered (inactive ones `hidden`), so all answers are in the page HTML for
// search and answer engines; the build also reads SUPPORT_FAQS for FAQPage structured data.
const SUPPORT_CATS = [
  { id: 'start', label: 'Getting started' },
  { id: 'record', label: 'Recording' },
  { id: 'schedule', label: 'Scheduling & delivery' },
  { id: 'recipients', label: 'Recipients' },
  { id: 'family', label: 'Family tree & sharing' },
  { id: 'privacy', label: 'Privacy & security' },
  { id: 'billing', label: 'Billing' },
  { id: 'after', label: 'When something happens to me' },
];

const SUPPORT_FAQS = {
  start: [
    { q: "How do I record my first memory?", a: "Sign up, press 'Add a memory' on your home screen, and choose a delivery type. You can pause, re-record, and save whenever you're ready. There's no wrong way to do this." },
    { q: "Do I need any special equipment?", a: "No. Your phone or laptop is fine. We'll use whatever camera and microphone you have, and we boost the audio quality automatically." },
    { q: "Can I try it without paying?", a: "Yes. Every account starts with a 7-day free trial, and you don't need a card. You can try every feature, though no messages are saved or sent until you subscribe." },
  ],
  record: [
    { q: "Can I re-record if I don't like the take?", a: "As many times as you'd like. We only keep what you save, and drafts stay private." },
    { q: "Is there a time limit per video?", a: "Individual recordings can be up to 30 minutes each. Monthly and yearly plans have no limit on total recording time." },
    { q: "Can I edit a recording after I save it?", a: "You can add a written note, change recipients, and reschedule it. We don't offer mid-video edits, because we want what you save to stay honest." },
  ],
  schedule: [
    { q: "What kinds of delivery dates can I set?", a: "A specific date, a recurring date (e.g. every birthday), a milestone (their 18th, their wedding), or a triggered event you define. You can change any of these at any time. Monthly and yearly plans schedule up to 12 months ahead; Legacy removes the limit." },
    { q: "Can I deliver a message immediately?", a: "Yes, you can send right away or schedule for later. Both work the same way." },
    { q: "What if a recipient isn't ready to receive it?", a: "We send a gentle email first, letting them know a message is waiting. They open it when they're ready. We never auto-play." },
  ],
  recipients: [
    { q: "How do recipients access their messages?", a: "They receive an email with a private link. They verify their identity the first time, and can revisit any unlocked message whenever they'd like." },
    { q: "Can I add or remove recipients later?", a: "Yes, on any message and at any time. Changes take effect immediately." },
  ],
  family: [
    { q: "How does the family tree work?", a: "Add the people in your family one at a time, with a photo, dates, and how they're related to you. Mark those who've passed, and view everyone as a tree or a simple list. Chosen family belongs here too." },
    { q: "Can my relatives add to my tree?", a: "Each person keeps their own tree, and only they can change it. Invite a relative by email, and once they join, their place on your tree fills in itself. If you share your tree with them, they can copy in the people they want. Anyone already on their tree is matched, not added twice." },
    { q: "Who can see the photos and stories I add?", a: "Only you, until you choose otherwise. Each photo and story has its own share setting. Tick it, and the relatives linked to your tree will find it under 'Shared with you'. You can change your mind at any time." },
  ],
  privacy: [
    { q: "Can Everlasting Memory watch my videos?", a: "No. Your messages are private and only ever delivered to the people you choose. The only exception is a genuine risk to life, a serious safeguarding concern, or a legal requirement, when we may have to review content." },
    { q: "Do you train AI on my recordings?", a: "No. We never use your recordings to train AI." },
    // CONFIRM with client: storage provider and region (Privacy policy still has a placeholder), then restore:
    // { q: "Where are my recordings stored?", a: "..." },
  ],
  billing: [
    { q: "What payment methods do you accept?", a: "All major cards through Stripe for all plans." },
    // CONFIRM with client: refund window per plan (Terms still has a placeholder), then put the policy back in this answer.
    { q: "Can I get a refund?", a: "Write to us at hello@everlasting-memory.com and a person will look at your account and reply, usually the same day." },
  ],
  after: [
    { q: "What happens to my account if I pass away?", a: "Messages you've scheduled are still sent, exactly as you planned. With Legacy, you name two Guardians. Once both confirm, the messages you've set aside for after you pass are delivered, and your legacy video stays on your family tree forever." },
    { q: "Can I leave instructions for my family?", a: "Yes. With Legacy, you can record messages to be delivered after you pass, released once both of your Guardians confirm." },
    // CONFIRM with client: succession plan, storage endowment and export, then restore:
    // { q: "What if Everlasting Memory shuts down?", a: "..." },
  ],
};

// On narrow screens the topic list becomes a sticky dropdown above the answers. After a
// change, bring the new answers into view if they're off screen, so the change is visible.
function supportRevealAnswers(el) {
  if (!el) return;
  const top = el.getBoundingClientRect().top, offset = 150;
  if (top < offset || top > window.innerHeight * 0.6) {
    const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    window.scrollTo({ top: top + window.scrollY - offset, behavior: reduce ? 'auto' : 'smooth' });
  }
}

function SupportPage({ navigate }) {
  const [cat, setCat] = React.useState('start');
  const titleRefs = React.useRef({});
  const chooseTopic = (id) => { setCat(id); requestAnimationFrame(() => supportRevealAnswers(titleRefs.current[id])); };

  return (
    <div>
      <section className="page-hero" data-em-surface="ink">
        <div className="page-hero__vignette" />
        <div className="page-hero__inner">
          <h1 className="page-hero__title">
            We answer our own emails.
          </h1>
          <p className="page-hero__lede">
            No tickets. No hold music. A real person, usually within a day. And a few things
            you can probably solve faster yourself, just below.
          </p>
        </div>
      </section>

      <section className="section support-section">
        <div className="container">
          <div className="support-grid">
            <aside className="support-side">
              <div className="support-side__label">Help topics</div>
              <div className="support-side__cats">
                {SUPPORT_CATS.map(c => (
                  <button
                    key={c.id}
                    className={"support-side__cat" + (c.id === cat ? " is-active" : "")}
                    onClick={() => setCat(c.id)}
                    aria-pressed={c.id === cat}
                  >{c.label}</button>
                ))}
              </div>
              <div className="support-side__contact">
                <h2>Still need a person?</h2>
                <p>Write to us. We read every message, and most people hear back the same day.</p>
                <a href="mailto:hello@everlasting-memory.com" className="btn btn--outline-ink btn--sm" style={{ width: '100%', justifyContent: 'center', display: 'flex' }}>
                  hello@everlasting-memory.com
                </a>
              </div>
            </aside>

            <div className="support-main">
              <div className="support-topic">
                <label className="support-topic__box">
                  <span className="support-topic__eyebrow">Topic</span>
                  <span className="support-topic__value" aria-hidden="true">{SUPPORT_CATS.find(c => c.id === cat).label}</span>
                  <span className="support-topic__chev" aria-hidden="true">▾</span>
                  <select aria-label="Help topic" value={cat} onChange={(e) => chooseTopic(e.target.value)}>
                    {SUPPORT_CATS.map(c => <option key={c.id} value={c.id}>{c.label}</option>)}
                  </select>
                </label>
              </div>
              {SUPPORT_CATS.map(c => (
                <div key={c.id} hidden={c.id !== cat}>
                  <h2 ref={el => (titleRefs.current[c.id] = el)} className="em-h1 support-main__title" style={{ fontSize: 36, marginTop: 8, marginBottom: 32 }}>{c.label}</h2>
                  {/* Re-keyed when shown, so the fade replays and the first answer starts open */}
                  <div className="faq" key={c.id === cat ? c.id + "-on" : c.id}>
                    {SUPPORT_FAQS[c.id].map((f, i) => (
                      <FaqItem key={i} q={f.q} a={f.a} defaultOpen={i === 0} />
                    ))}
                  </div>
                </div>
              ))}
            </div>
          </div>
        </div>
      </section>
    </div>
  );
}

window.SupportPage = SupportPage;
