// Privacy modal — per portfolio standard.
// Triggered from footer link. No URL change. ESC + click-outside dismiss.
// Focus trap inside modal; focus returns to trigger on close.

function PrivacyModal({ open, onClose, triggerRef }) {
  const modalRef = React.useRef(null);
  const closeBtnRef = React.useRef(null);

  // Lock body scroll
  React.useEffect(() => {
    if (!open) return;
    const prev = document.body.style.overflow;
    document.body.style.overflow = 'hidden';
    return () => { document.body.style.overflow = prev; };
  }, [open]);

  // Focus management — focus close btn on open; restore to trigger on close
  React.useEffect(() => {
    if (!open) return;
    const t = setTimeout(() => closeBtnRef.current?.focus(), 0);
    return () => {
      clearTimeout(t);
      triggerRef?.current?.focus?.();
    };
  }, [open, triggerRef]);

  // ESC + focus trap
  React.useEffect(() => {
    if (!open) return;
    const onKey = (e) => {
      if (e.key === 'Escape') {
        e.stopPropagation();
        onClose();
        return;
      }
      if (e.key === 'Tab' && modalRef.current) {
        const focusables = modalRef.current.querySelectorAll(
          'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
        );
        if (focusables.length === 0) return;
        const first = focusables[0];
        const last = focusables[focusables.length - 1];
        if (e.shiftKey && document.activeElement === first) {
          e.preventDefault();
          last.focus();
        } else if (!e.shiftKey && document.activeElement === last) {
          e.preventDefault();
          first.focus();
        }
      }
    };
    document.addEventListener('keydown', onKey);
    return () => document.removeEventListener('keydown', onKey);
  }, [open, onClose]);

  if (!open) return null;

  return (
    <div
      className="privacy-scrim"
      onClick={(e) => { if (e.target === e.currentTarget) onClose(); }}
    >
      <div
        ref={modalRef}
        className="privacy-modal"
        role="dialog"
        aria-modal="true"
        aria-labelledby="privacy-title"
      >
        <header className="privacy-modal-head">
          <div>
            <div className="privacy-modal-eyebrow">S-Corp Breakeven</div>
            <h2 id="privacy-title" className="privacy-modal-title">Privacy</h2>
          </div>
          <button
            ref={closeBtnRef}
            className="privacy-modal-x"
            onClick={onClose}
            aria-label="Close privacy"
          >×</button>
        </header>

        <div className="privacy-modal-body">
          <blockquote className="privacy-tldr">
            <strong>The short version:</strong> we don't want your data, we don't
            ask for an account, and your income, state, salary, and other tax
            inputs never leave your browser. Use this page like you would a
            back-of-the-envelope calculator.
          </blockquote>

          <h3>What we don't collect</h3>
          <ul>
            <li>No account, no email, no name, no phone number.</li>
            <li>
              Your income, state, salary, and other tax inputs are{' '}
              <strong>processed entirely in your browser</strong>. They are
              never transmitted to our servers.
            </li>
            <li>
              We don't sell, share, or rent any user data — we don't have
              any to sell.
            </li>
          </ul>

          <h3>What we do collect</h3>
          <ul>
            <li>
              <strong>Anonymous usage analytics</strong> — page views, browser
              type, country (from IP, not stored). Aggregated only; not tied
              to any individual.
            </li>
          </ul>

          <h3>Advertising</h3>
          <p>
            This site is free because it's supported by display advertising.
            Our advertising partners may set their own cookies on your device
            to serve relevant ads and measure performance. We do not share
            any of your income, state, salary, or other tax inputs with those
            partners.
          </p>
          <p>
            We explicitly do not share any financial information you enter on
            this site with any advertising or analytics partner.
          </p>
          <p>
            You can opt out of personalized advertising via{' '}
            <a href="https://aboutads.info" target="_blank" rel="noopener">aboutads.info</a>{' '}
            or your browser's privacy controls.
          </p>
          <p>
            EU/UK visitors will see a consent prompt before any non-essential
            cookies are set.
          </p>

          <h3>Deleting your data</h3>
          <p>
            Because we don't store anything server-side, the only place your
            data exists is on your device. To delete it, clear your browser's
            site data for scorpbreakeven.com.
          </p>

          <h3>California residents</h3>
          <p>
            California residents have the right to opt out of the "sale" of
            personal information. Under CCPA's broad definition, our use of
            behavioral advertising cookies may qualify. To opt out, use the
            cookie consent controls or enable the Global Privacy Control
            signal in your browser — we honor GPC.
          </p>

          <h3>Children</h3>
          <p>
            This site is not directed to children under 13, and we do not
            knowingly collect any information from children. If you believe a
            child has provided us with information, contact us at the address
            below and we will delete it.
          </p>

          <h3>Third-party links</h3>
          <p>
            Links to the IRS, RCReports, BizStats, and 10yearrmd.com lead to
            their own websites; we don't control or speak for their privacy
            practices.
          </p>

          <h3>Contact</h3>
          <p>
            For privacy questions, email{' '}
            <a href="mailto:privacy@scorpbreakeven.com">privacy@scorpbreakeven.com</a>.
          </p>

          <p className="privacy-updated">Last updated: May 24, 2026</p>

          <div className="privacy-modal-actions">
            <button className="btn-primary" onClick={onClose}>Close</button>
          </div>
        </div>
      </div>
    </div>
  );
}

window.PrivacyModal = PrivacyModal;
