/* Mobile: Free School Clinics page + school request form */

const CLINIC_WEB3FORMS_KEY = "b8d90ff4-9ffd-422f-a59d-c0c2d5e120c6";

const CLINIC_FACTS = [
  ["Free", "No cost to the school"],
  ["Grades 1–8", "Age-appropriate groups"],
  ["75 min", "One class block"],
  ["At your school", "We bring everything"],
];

const CLINIC_INCLUDES = [
  ["Coaches on site", "LOGIC coaches lead every station — your staff just supervise."],
  ["Equipment provided", "Balls and cones come with us. You provide the gym."],
  ["Fundamentals + play", "Ball handling, passing, shooting and small-sided games, scaled by grade."],
  ["More than basketball", "Every session closes on teamwork, effort and confidence — the LOGIC way."],
];

const CLINIC_STEPS = [
  ["Submit your school", "Tell us your school name, address and a contact email."],
  ["We reach out", "Coach AD will reach out to find a date."],
  ["Clinic day", "We arrive early, set up, run the clinic and pack up."],
];

function ClinicsPage({ onGo }) {
  return (
    <main className="page">
      <section style={{ padding: "16px var(--gutter) 26px" }}>
        <span className="eyebrow">Free school program · London elementary</span>
        <h1 className="display" style={{ margin: "12px 0 0", fontSize: 56, lineHeight: 0.9, color: "#fff" }}>
          Free school <span style={{ color: "var(--c-red)" }}>clinics.</span>
        </h1>
        <p style={{ marginTop: 14, color: "var(--c-ink-soft)", fontSize: 15.5, lineHeight: 1.6 }}>
          We're offering <strong style={{ color: "#fff" }}>free basketball clinics</strong> to elementary schools across London.
          Our coaches come to you — bring your gym and your students, we handle the rest.
        </p>
        <div style={{ display: "flex", flexDirection: "column", gap: 10, marginTop: 22 }}>
          <button className="btn tappable" onClick={() => onGo("clinic-request")}>Request a clinic for your school →</button>
          <a href="https://calendly.com/coach_ad" target="_blank" rel="noopener noreferrer" className="btn btn-ghost tappable">Book a call with Coach AD</a>
        </div>
      </section>

      <section style={{ padding: "0 var(--gutter) 26px" }}>
        <MSlot label="School clinic — coach and young athlete" ratio="16 / 10" src="../assets/school-clinic.jpg" />
      </section>

      <section style={{ padding: "0 var(--gutter) 30px" }}>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", border: "1px solid var(--c-line)", borderRadius: 14, overflow: "hidden" }}>
          {CLINIC_FACTS.map(([k, v], i) => (
            <div key={k} style={{ padding: "18px 16px", borderRight: i % 2 === 0 ? "1px solid var(--c-line)" : "none", borderBottom: i < 2 ? "1px solid var(--c-line)" : "none" }}>
              <div className="display" style={{ fontSize: 26, lineHeight: 0.95, color: "var(--c-red)" }}>{k}</div>
              <div className="mono" style={{ marginTop: 6, color: "var(--c-ink-soft)", fontSize: 10 }}>{v}</div>
            </div>
          ))}
        </div>
      </section>

      <section style={{ padding: "30px var(--gutter) 30px", borderTop: "1px solid var(--c-line)", background: "var(--c-paper)" }}>
        <MHead eyebrow="What's included">What your students <span style={{ color: "var(--c-red)" }}>get.</span></MHead>
        <div style={{ display: "grid", gap: 10 }}>
          {CLINIC_INCLUDES.map(([t, b], i) => (
            <div key={t} className="card" style={{ background: "var(--c-bg)", padding: "18px 18px", display: "flex", gap: 14 }}>
              <span className="mono" style={{ color: "var(--c-red)", fontSize: 10, paddingTop: 4 }}>{String(i + 1).padStart(2, "0")}</span>
              <div>
                <div className="display" style={{ fontSize: 20, lineHeight: 1.05, color: "#fff" }}>{t}</div>
                <p style={{ margin: "6px 0 0", color: "var(--c-ink-soft)", fontSize: 14, lineHeight: 1.55 }}>{b}</p>
              </div>
            </div>
          ))}
        </div>
      </section>

      <section style={{ padding: "30px var(--gutter) 12px" }}>
        <MHead eyebrow="How it works">Three steps to <span style={{ color: "var(--c-red)" }}>your gym.</span></MHead>
        <ol style={{ listStyle: "none", margin: 0, padding: 0, display: "grid", gap: 0 }}>
          {CLINIC_STEPS.map(([t, b], i) => (
            <li key={t} style={{ display: "flex", gap: 14, padding: "16px 0", borderTop: "1px solid var(--c-line)" }}>
              <span style={{ flex: "0 0 auto", width: 30, height: 30, borderRadius: 999, background: "var(--c-red)", color: "#fff", display: "grid", placeItems: "center", fontFamily: "var(--font-mono)", fontSize: 12, fontWeight: 600 }}>{i + 1}</span>
              <div>
                <div className="display" style={{ fontSize: 19, lineHeight: 1.1, color: "#fff" }}>{t}</div>
                <p style={{ margin: "4px 0 0", color: "var(--c-ink-soft)", fontSize: 14, lineHeight: 1.5 }}>{b}</p>
              </div>
            </li>
          ))}
        </ol>
      </section>

      <section style={{ padding: "8px var(--gutter) 30px" }}>
        <MSlot label="Coach defending a clinic athlete" ratio="16 / 10" src="../assets/clinic-coaching.jpg" />
        <div className="card card-red" style={{ padding: "26px 22px", marginTop: 12 }}>
          <span className="mono" style={{ color: "rgba(255,255,255,0.9)", fontSize: 11 }}>Spots are limited each term</span>
          <div className="display" style={{ marginTop: 10, fontSize: 36, lineHeight: 0.95, color: "#fff" }}>Bring LOGIC to your school.</div>
          <p style={{ marginTop: 12, color: "rgba(255,255,255,0.94)", fontSize: 14.5, lineHeight: 1.55 }}>
            Takes under a minute. We'll follow up to confirm a date that works for your school.
          </p>
          <button className="btn btn-inverse tappable" style={{ marginTop: 18 }} onClick={() => onGo("clinic-request")}>Request a clinic →</button>
        </div>
      </section>
    </main>
  );
}

function ClinicField({ label, value, onChange, type = "text", placeholder, optional, multiline }) {
  const base = { padding: "14px 14px", borderRadius: 10, border: "1.5px solid var(--c-line)", background: "#0a0a0a", color: "#fff", fontFamily: "inherit", fontSize: 16, WebkitAppearance: "none", width: "100%" };
  return (
    <label style={{ display: "flex", flexDirection: "column", gap: 6 }}>
      <span className="mono" style={{ color: "var(--c-ink-soft)", fontSize: 9.5 }}>{label}{optional && <span style={{ color: "var(--c-ink-dim)" }}> · optional</span>}</span>
      {multiline
        ? <textarea value={value} onChange={e => onChange(e.target.value)} placeholder={placeholder} style={{ ...base, minHeight: 110, resize: "vertical", lineHeight: 1.5 }} />
        : <input value={value} onChange={e => onChange(e.target.value)} type={type} placeholder={placeholder} style={base} />}
    </label>
  );
}

function ClinicRequestPage({ onGo }) {
  const [d, setD] = React.useState({
    schoolName: "", street: "", city: "London", province: "ON",
    contactName: "", email: "", phone: "", notes: "",
  });
  const [sending, setSending] = React.useState(false);
  const [error, setError] = React.useState(null);
  const [done, setDone] = React.useState(false);
  const up = (k) => (v) => setD(s => ({ ...s, [k]: v }));
  const emailOk = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(d.email);
  const valid = d.schoolName.trim() && d.street.trim() && d.city.trim() && d.contactName.trim() && emailOk;

  React.useEffect(() => { window.scrollTo({ top: 0, behavior: "smooth" }); }, [done]);

  async function submit() {
    if (!valid || sending) return;
    setSending(true); setError(null);
    try {
      const res = await fetch("https://api.web3forms.com/submit", {
        method: "POST",
        headers: { "Content-Type": "application/json", Accept: "application/json" },
        body: JSON.stringify({
          access_key: CLINIC_WEB3FORMS_KEY,
          subject: `[School Clinic Request] ${d.schoolName}`,
          from_name: "London LOGIC Basketball — School Clinic Form (Mobile)",
          "School Name": d.schoolName,
          "School Address": [d.street, d.city, d.province].filter(Boolean).join(", "),
          "Contact Name": d.contactName,
          "Contact Email": d.email,
          "Contact Phone": d.phone || "—",
          "Notes": d.notes || "—",
          replyto: d.email,
        }),
      });
      const json = await res.json().catch(() => ({}));
      if (!res.ok || !json.success) throw new Error(json.message || `Submission failed (${res.status})`);
      setDone(true);
    } catch (e) {
      setError(e.message || "Network error.");
    } finally {
      setSending(false);
    }
  }

  if (done) return (
    <main className="page">
      <section style={{ padding: "60px var(--gutter) 30px", textAlign: "center" }}>
        <span className="eyebrow" style={{ justifyContent: "center" }}>Request received</span>
        <h1 className="display" style={{ margin: "16px 0 0", fontSize: 64, lineHeight: 0.92, color: "#fff" }}>
          See you <span style={{ color: "var(--c-red)" }}>in the gym.</span>
        </h1>
        <p style={{ marginTop: 16, color: "var(--c-ink-soft)", fontSize: 15, lineHeight: 1.55 }}>
          Thanks — we've logged <strong style={{ color: "#fff" }}>{d.schoolName}</strong>. Coach AD will email{" "}
          <span style={{ color: "#fff" }}>{d.email}</span> to find a date.
        </p>
      </section>
      <section style={{ padding: "0 var(--gutter) 24px", display: "flex", flexDirection: "column", gap: 10 }}>
        <button className="btn tappable" onClick={() => onGo("clinics")}>Back to school clinics</button>
        <button className="btn btn-ghost tappable" onClick={() => onGo("home")}>Back home</button>
      </section>
    </main>
  );

  return (
    <main className="page">
      <section style={{ padding: "10px var(--gutter) 18px" }}>
        <a onClick={() => onGo("clinics")} className="mono tappable" style={{ color: "var(--c-ink-soft)", fontSize: 10.5, cursor: "pointer", display: "inline-block", padding: "4px 0" }}>← School clinics</a>
        <span className="eyebrow" style={{ display: "flex", marginTop: 12 }}>Free school clinic · request</span>
        <h1 className="display" style={{ margin: "10px 0 0", fontSize: 46, lineHeight: 0.95 }}>
          Bring LOGIC to <span style={{ color: "var(--c-red)" }}>your school.</span>
        </h1>
        <p style={{ marginTop: 12, color: "var(--c-ink-soft)", fontSize: 14.5, lineHeight: 1.55 }}>
          Free for London elementary schools. Tell us where you are and who to contact — we'll handle the rest.
        </p>
      </section>

      <section style={{ padding: "0 var(--gutter) 16px" }}>
        <div style={{ background: "#15110d", color: "#fff", borderRadius: "12px 12px 0 0", padding: "11px 16px" }}>
          <span className="mono" style={{ fontSize: 10, letterSpacing: "0.12em", color: "#fff" }}>School clinic request · $0</span>
        </div>
        <div className="card" style={{ padding: "22px 20px", borderTopLeftRadius: 0, borderTopRightRadius: 0, display: "flex", flexDirection: "column", gap: 22 }}>
          <div>
            <span className="mono" style={{ color: "var(--c-red)" }}>01 · School</span>
            <div style={{ display: "flex", flexDirection: "column", gap: 14, marginTop: 12 }}>
              <ClinicField label="School name" value={d.schoolName} onChange={up("schoolName")} placeholder="e.g. Northridge Public School" />
              <ClinicField label="Street address" value={d.street} onChange={up("street")} />
              <ClinicField label="City" value={d.city} onChange={up("city")} />
              <ClinicField label="Province" value={d.province} onChange={up("province")} />
            </div>
          </div>
          <div>
            <span className="mono" style={{ color: "var(--c-red)" }}>02 · Contact</span>
            <div style={{ display: "flex", flexDirection: "column", gap: 14, marginTop: 12 }}>
              <ClinicField label="Contact name" value={d.contactName} onChange={up("contactName")} placeholder="Teacher, principal or coordinator" />
              <ClinicField label="Contact email" type="email" value={d.email} onChange={up("email")} placeholder="name@school.ca" />
              <ClinicField label="Phone" type="tel" value={d.phone} onChange={up("phone")} optional />
              <ClinicField label="Anything we should know?" value={d.notes} onChange={up("notes")} optional multiline placeholder="Grades, number of students, preferred dates…" />
            </div>
          </div>
          {error && (
            <div style={{ padding: "12px 14px", borderRadius: 10, border: "1px solid var(--c-red)", background: "rgba(230,24,44,0.08)", fontSize: 13, lineHeight: 1.45 }}>
              <strong style={{ color: "var(--c-red)" }}>Couldn't send.</strong> {error} Try again, or email{" "}
              <a href="mailto:coach_ad@londonlogicbasketball.com?subject=Free%20school%20clinic%20enquiry" style={{ color: "#fff", textDecoration: "underline" }}>coach_ad@londonlogicbasketball.com</a>.
            </div>
          )}
          <button className="btn tappable" onClick={submit} disabled={!valid || sending}
            style={{ opacity: valid ? 1 : 0.4, cursor: valid && !sending ? "pointer" : "default" }}>
            {sending ? "Sending…" : "Submit clinic request →"}
          </button>
        </div>
      </section>
    </main>
  );
}

Object.assign(window, { ClinicsPage, ClinicRequestPage });
