const { Card, Button, Icon, Input, Callout, Divider } = window.BowcareDesignSystem_68307b;
const { WRAP, Eyebrow } = window;

/* --- FAQ: disclosure buttons with aria-expanded / aria-controls --- */
const FAQS = [
  { q: 'Do I need a smartphone?', a: 'A phone or a tablet works. Bowcare also opens in a web browser on a computer, with the same large print.' },
  { q: 'Can I record what the doctor says?', a: 'Yes, if you tell the doctor first. The recording stays on your phone and is attached to that visit. You can delete it any time.' },
  { q: 'Who else can see my visits?', a: 'Only the people you add, and only what you choose to share. They can read; they cannot change your notes.' },
  { q: 'What does it cost?', a: 'Keeping visits, notes and takeaways is free. Printed summaries and recording storage are part of the paid plan.' },
];

function Faq() {
  const [open, setOpen] = React.useState(0);
  return (
    <section id="faq" aria-labelledby="faq-h" style={{ padding: '80px 0' }}>
      <div style={{ ...WRAP, maxWidth: 820 }}>
        <Eyebrow>Questions</Eyebrow>
        <h2 id="faq-h" style={{ font: 'var(--weight-medium) 40px/1.15 var(--font-serif-display)', color: 'var(--text-strong)', margin: '0 0 32px' }}>
          What people ask first.
        </h2>
        <div style={{ display: 'grid', gap: 12 }}>
          {FAQS.map((f, i) => {
            const isOpen = open === i;
            return (
              <Card key={f.q} padding="0">
                <h3 style={{ margin: 0 }}>
                  <button type="button" aria-expanded={isOpen} aria-controls={'faq-p-' + i} id={'faq-b-' + i}
                    onClick={() => setOpen(isOpen ? -1 : i)}
                    style={{
                      width: '100%', minHeight: 64, display: 'flex', alignItems: 'center', justifyContent: 'space-between',
                      gap: 20, padding: '16px 24px', background: 'none', border: 0, cursor: 'pointer',
                      font: 'var(--text-title-3)', color: 'var(--text-strong)', textAlign: 'left',
                    }}>
                    <span>{f.q}</span>
                    <Icon name={isOpen ? 'minus' : 'plus'} size={26} color="var(--sage-600)" />
                  </button>
                </h3>
                {isOpen ? (
                  <div id={'faq-p-' + i} role="region" aria-labelledby={'faq-b-' + i}
                    style={{ padding: '0 24px 24px', font: 'var(--text-body-lg)', color: 'var(--text-muted)', maxWidth: '60ch' }}>
                    {f.a}
                  </div>
                ) : null}
              </Card>
            );
          })}
        </div>
      </div>
    </section>
  );
}

/* --- Accessibility statement: AODA requires a public, reachable commitment --- */
const A11Y = [
  ['Standard met', 'WCAG 2.1 Level AA, and the design targets AAA contrast for body text.'],
  ['Ontario (AODA)', 'This site and the app follow the Integrated Accessibility Standards Regulation for information and communications.'],
  ['Text and zoom', 'Body text starts at 18px and reflows to 400% zoom with no loss of content.'],
  ['Keyboard', 'Every control is reachable and operable by keyboard, with a visible 3px focus ring.'],
  ['Screen readers', 'Tested with VoiceOver and NVDA. Status messages are announced, not just coloured.'],
  ['Feedback', 'Tell us where we fall short: access@bowcare.example — we reply within 2 business days, in the format you ask for.'],
];

function AccessibilityStatement() {
  return (
    <section id="access" aria-labelledby="access-h" style={{ background: 'var(--sage-50)', borderTop: '1px solid var(--sage-100)', padding: '80px 0' }}>
      <div style={WRAP}>
        <Eyebrow>Accessibility</Eyebrow>
        <h2 id="access-h" style={{ font: 'var(--weight-medium) 40px/1.15 var(--font-serif-display)', color: 'var(--text-strong)', margin: '0 0 12px' }}>
          Accessible because of who uses it.
        </h2>
        <p style={{ font: 'var(--text-body-lg)', color: 'var(--text-body)', maxWidth: '60ch', margin: '0 0 36px' }}>
          Bowcare is used by people with low vision, tremor, hearing loss and memory trouble. Accessibility is the product, not a compliance exercise.
        </p>
        <dl style={{ margin: 0, display: 'grid', gridTemplateColumns: 'repeat(2,minmax(0,1fr))', gap: '28px 48px' }}>
          {A11Y.map(([t, d]) => (
            <div key={t}>
              <dt style={{ font: 'var(--text-label)', color: 'var(--sage-700)', marginBottom: 6 }}>{t}</dt>
              <dd style={{ font: 'var(--text-body-default)', color: 'var(--text-body)', margin: 0 }}>{d}</dd>
            </div>
          ))}
        </dl>
      </div>
    </section>
  );
}

/* --- Sign-up: label, hint, error identification (3.3.1) and confirmation --- */
function SignUp() {
  const [email, setEmail] = React.useState('');
  const [err, setErr] = React.useState('');
  const [done, setDone] = React.useState(false);
  const submit = (e) => {
    e.preventDefault();
    if (!/.+@.+\..+/.test(email)) { setErr('That address is missing an @ or a dot. Check it and try again.'); setDone(false); return; }
    setErr(''); setDone(true);
  };
  return (
    <section id="start" aria-labelledby="start-h" style={{ padding: '80px 0' }}>
      <div style={{ ...WRAP, maxWidth: 720, textAlign: 'center' }}>
        <h2 id="start-h" style={{ font: 'var(--weight-medium) 40px/1.15 var(--font-serif-display)', color: 'var(--text-strong)', margin: '0 0 12px' }}>
          Start with the next visit.
        </h2>
        <p style={{ font: 'var(--text-body-lg)', color: 'var(--text-muted)', margin: '0 auto 32px', maxWidth: '46ch' }}>
          Put in your email and we will send a link to set up Bowcare on your phone.
        </p>
        <form onSubmit={submit} noValidate style={{ display: 'grid', gap: 16, justifyItems: 'stretch', textAlign: 'left' }}>
          <Input label="Your email address" hint="We use it once, to send the link" type="email"
            value={email} onChange={(e) => setEmail(e.target.value)} error={err || undefined} />
          <Button size="large" type="submit" fullWidth>Send me the link</Button>
        </form>
        <div aria-live="polite" style={{ marginTop: 20, textAlign: 'left' }}>
          {done ? <Callout tone="go" title="Check your email">We sent a link to {email}. It works for 24 hours.</Callout> : null}
        </div>
      </div>
    </section>
  );
}

function SiteFooter() {
  const cols = [
    ['Bowcare', ['How it works', 'Sample summary', 'Pricing']],
    ['For family', ['Helping a parent', 'Printing summaries', 'Sharing safely']],
    ['Company', ['About', 'Privacy', 'Accessibility']],
  ];
  return (
    <footer style={{ background: 'var(--surface-inverse)', color: 'var(--text-on-dark)', padding: '56px 0 40px' }}>
      <div style={{ ...WRAP, display: 'grid', gridTemplateColumns: 'minmax(0,1.2fr) repeat(3,minmax(0,1fr))', gap: 40 }}>
        <div>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 12, minHeight: 48 }}>
            <span style={{ width: 48, height: 48, display: 'grid', placeItems: 'center', borderRadius: 'var(--radius-sm)', background: 'var(--paper-0)' }}>
              <img src="assets/logo-mark.png" alt="" width="34" height="30" style={{ display: 'block' }} />
            </span>
            <span style={{ font: 'var(--weight-medium) 30px/1 var(--font-serif-display)', letterSpacing: '-0.02em' }}>Bowcare</span>
          </span>
          <p style={{ font: 'var(--text-body-default)', color: 'var(--sage-100)', margin: '12px 0 0', maxWidth: '28ch' }}>
            Visit notes that make sense a month later.
          </p>
        </div>
        {cols.map(([h, links]) => (
          <nav key={h} aria-label={h}>
            <h2 style={{ font: 'var(--text-label)', color: 'var(--paper-0)', margin: '0 0 14px' }}>{h}</h2>
            <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'grid', gap: 0 }}>
              {links.map((l) => (
                <li key={l}>
                  <a href="#top" style={{ display: 'inline-flex', alignItems: 'center', minHeight: 48, font: 'var(--text-body-default)', color: 'var(--sage-100)', textDecoration: 'none' }}>{l}</a>
                </li>
              ))}
            </ul>
          </nav>
        ))}
      </div>
      <div style={{ ...WRAP, marginTop: 40, paddingTop: 24, borderTop: '1px solid var(--sage-600)' }}>
        <p style={{ font: 'var(--text-caption)', color: 'var(--sage-200)', margin: 0 }}>
          © 2026 Bowcare · WCAG 2.1 AA · AODA compliant · access@bowcare.example
        </p>
      </div>
    </footer>
  );
}

Object.assign(window, { Faq, AccessibilityStatement, SignUp, SiteFooter });
