// Minutes.jsx — inaugural meeting readout lead-magnet page.
//
// The form captures an email and POSTs it cross-origin to the Aineko dashboard
// lead-magnet webhook (which holds all secrets server-side; nothing sensitive
// ships in this static page). On success it hands the visitor the public
// readout PDF. The asset served here is the FINALIZED, sanitized inaugural
// readout, not the raw "Draft for attendee review" minutes.
const WEBHOOK = "https://aineko-dashboard.vercel.app/api/webhook/lead-magnet";
const READOUT_PDF = "assets/OIPC_Inaugural_Readout_2026-06-25.pdf";
const CONSENT_TEXT =
  "I agree to receive periodic email updates from the Open IP Council. I can withdraw consent and unsubscribe at any time.";

const field = {
  width: "100%",
  fontFamily: "var(--font-sans)",
  fontSize: 15,
  color: "var(--ink)",
  background: "var(--paper-white)",
  border: "1px solid var(--rule-2, #d2d8e4)",
  borderRadius: 2,
  padding: "11px 13px",
  marginTop: 6,
  boxSizing: "border-box",
};
const label = { fontFamily: "var(--font-sans)", fontSize: 13, fontWeight: 600, color: "var(--ink-2)" };

function MinutesPage({ onNavigate }) {
  const [name, setName] = React.useState("");
  const [email, setEmail] = React.useState("");
  const [company, setCompany] = React.useState("");
  const [agree, setAgree] = React.useState(false);
  const [status, setStatus] = React.useState("idle"); // idle | sending | done | error
  const [warn, setWarn] = React.useState(false);

  const emailOk = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim());
  const canSubmit = emailOk && agree && status !== "sending";

  function triggerDownload() {
    const a = document.createElement("a");
    a.href = READOUT_PDF;
    a.download = "OIPC_Inaugural_Readout_2026-06-25.pdf";
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  }

  async function onSubmit(e) {
    e.preventDefault();
    if (!canSubmit) return;
    setStatus("sending");
    setWarn(false);
    try {
      const res = await fetch(WEBHOOK, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          brand: "oipc",
          name: name.trim() || email.trim(),
          email: email.trim(),
          company: company.trim() || null,
          source_url: "https://openipcouncil.org/minutes",
          lead_magnet: "oipc_inaugural_minutes",
          consent: true,
          consent_text: CONSENT_TEXT,
        }),
      });
      if (!res.ok) setWarn(true);
    } catch (err) {
      setWarn(true);
    }
    setStatus("done");
    triggerDownload();
  }

  return (
    <div>
      <SectionOpener
        section="Resource · Inaugural meeting"
        version="v0.1"
        date="2026-06-25"
        title="Inaugural meeting readout."
        standfirst="A short, public readout from the first working session of the Open IP Council, held June 25, 2026. Enter your email to download it and to follow the standard as it takes shape."
      />

      <div className="container section doc-narrow">
        <p style={{ fontFamily: "var(--font-serif)", fontSize: 19, lineHeight: 1.6, color: "var(--ink)" }}>
          The first session brought together prosecutors, in-house counsel, an engineer with a
          standards background, and a patent-valuation specialist. The group agreed on the core
          problem: there is no shared standard for evaluating the AI tools now entering patent
          practice, which leaves buyers trusting vendor claims and firms carrying unbounded risk.
        </p>

        <ul style={{ fontFamily: "var(--font-sans)", fontSize: 16, lineHeight: 1.7, color: "var(--ink-2)", paddingLeft: 20 }}>
          <li>Insurers cannot price AI risk for a firm without a standard to classify a tool against. Until one exists, the firm carries the liability.</li>
          <li>Hallucination sanctions already exist in litigation. The group expects them in prosecution within two to three years.</li>
          <li>A benchmark lets a firm show it followed a standard, which supports insurance, client security demands, and a defense against any claim of recklessness.</li>
          <li>Procurement is moving the same way, with new modules able to enforce a security standard across the legal software a firm buys.</li>
        </ul>

        {status === "done" ? (
          <div className="doc-callout" style={{ marginTop: 32 }}>
            <div className="doc-callout-eyebrow">Thank you</div>
            <div className="doc-callout-body">
              Your download is starting. If it does not begin,{" "}
              <a className="body-link" href={READOUT_PDF} download>download the readout here</a>.
              {warn && (
                <div style={{ marginTop: 10, color: "var(--ink-3)", fontSize: 14 }}>
                  We could not confirm your email was added to the list. You still have the readout;
                  please email contact@openipcouncil.org if you would like to be added.
                </div>
              )}
            </div>
          </div>
        ) : (
          <form onSubmit={onSubmit} style={{ marginTop: 32, maxWidth: 520 }}>
            <div style={{ marginBottom: 16 }}>
              <label style={label} htmlFor="oipc-name">Name</label>
              <input id="oipc-name" style={field} type="text" value={name}
                     onChange={(e) => setName(e.target.value)} placeholder="Optional" />
            </div>
            <div style={{ marginBottom: 16 }}>
              <label style={label} htmlFor="oipc-email">Work email</label>
              <input id="oipc-email" style={field} type="email" required value={email}
                     onChange={(e) => setEmail(e.target.value)} placeholder="you@firm.com" />
            </div>
            <div style={{ marginBottom: 16 }}>
              <label style={label} htmlFor="oipc-company">Firm or organization</label>
              <input id="oipc-company" style={field} type="text" value={company}
                     onChange={(e) => setCompany(e.target.value)} placeholder="Optional" />
            </div>

            <label style={{ display: "flex", gap: 10, alignItems: "flex-start", fontFamily: "var(--font-sans)", fontSize: 14, color: "var(--ink-2)", lineHeight: 1.5, marginBottom: 18 }}>
              <input type="checkbox" required checked={agree}
                     onChange={(e) => setAgree(e.target.checked)} style={{ marginTop: 3 }} />
              <span>{CONSENT_TEXT}</span>
            </label>

            <button className="btn btn-primary" type="submit" disabled={!canSubmit}
                    style={{ opacity: canSubmit ? 1 : 0.5, cursor: canSubmit ? "pointer" : "not-allowed" }}>
              {status === "sending" ? "Sending…" : "Download the readout"}
            </button>
          </form>
        )}

        <div className="doc-callout" style={{ marginTop: 40 }}>
          <div className="doc-callout-eyebrow">How we use your email</div>
          <div className="doc-callout-body">
            The Open IP Council uses your email only to send periodic updates about the standard:
            meeting readouts, charter and methodology developments, and occasional invitations. We
            do not sell or share it, and we do not use tracking pixels. You can unsubscribe at any
            time from the link in any email or on the{" "}
            <a className="body-link" href="#" onClick={(e) => { e.preventDefault(); if (onNavigate) onNavigate("unsubscribe"); }}>unsubscribe page</a>.
            Questions: contact@openipcouncil.org.
          </div>
        </div>
      </div>
    </div>
  );
}
window.MinutesPage = MinutesPage;
