// Contact page

const ContactPage = ({ navigate }) => {
  const [form, setForm] = React.useState({
    name: "", email: "", phone: "", topic: "Rehab & Performance Coaching", message: "",
  });
  const [errors, setErrors] = React.useState({});
  const [sent, setSent] = React.useState(false);
  const [submitting, setSubmitting] = React.useState(false);
  const [submitError, setSubmitError] = React.useState(null);

  const set = (k, v) => {
    setForm((f) => ({ ...f, [k]: v }));
    if (errors[k]) setErrors((e) => ({ ...e, [k]: null }));
  };

  const validate = () => {
    const e = {};
    if (!form.name.trim()) e.name = "Please share your name.";
    if (!form.email.trim()) e.email = "Email is required.";
    else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) e.email = "Hmm, that doesn't look right.";
    if (!form.message.trim()) e.message = "A short message helps us reply usefully.";
    else if (form.message.trim().length < 10) e.message = "A little more detail, please.";
    return e;
  };

  const submit = async (ev) => {
    ev.preventDefault();
    const e = validate();
    if (Object.keys(e).length) { setErrors(e); return; }
    if (submitting) return;
    setSubmitting(true);
    setSubmitError(null);
    try {
      const res = await fetch("https://formspree.io/f/mqeowyzr", {
        method: "POST",
        headers: { "Content-Type": "application/json", Accept: "application/json" },
        body: JSON.stringify({
          name: form.name,
          email: form.email,
          phone: form.phone || "\u2014",
          topic: form.topic,
          message: form.message,
          _subject: `New contact message \u2014 ${form.topic} \u2014 ${form.name}`,
        }),
      });
      if (res.ok) {
        setSent(true);
      } else {
        const data = await res.json().catch(() => ({}));
        setSubmitError(
          (data.errors && data.errors.map((x) => x.message).join(", ")) ||
          "Something went wrong. Please try again, or email info@7araka.co directly."
        );
      }
    } catch (err) {
      setSubmitError("Network error \u2014 please check your connection and try again.");
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <Page>
      {/* ---------- Hero ---------- */}
      <section style={{ padding: "72px 0 24px" }}>
        <div className="container-wide">
          <Eyebrow>Contact</Eyebrow>
          <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 64, marginTop: 20, alignItems: "end" }}>
            <h1 className="display-mega text-balance" style={{ maxWidth: 880 }}>
              Let's talk.
              <br />
              <span style={{ color: "var(--muted)" }}>No pressure, no pitch.</span>
            </h1>
            <p className="text-body-lg text-pretty">
              Tell us where you're at. We'll reply within 48 hours with honest next steps, even if 7ARAKA isn't the right fit.
            </p>
          </div>
        </div>
      </section>

      {/* ---------- Form + sidebar ---------- */}
      <section style={{ padding: "48px 0 64px" }}>
        <div className="container-wide" style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 32 }}>
          <div className="card" style={{ padding: 36 }}>
            {sent ? (
              <div style={{ padding: "20px 0", textAlign: "center" }}>
                <div style={{
                  width: 56, height: 56, borderRadius: 999,
                  background: "var(--cursor-primary)", color: "var(--ink)",
                  display: "grid", placeItems: "center", margin: "0 auto 16px"
                }}>
                  <Icon name="check" size={22} stroke={2} />
                </div>
                <h4 className="display-md">Message received.</h4>
                <p className="text-body" style={{ marginTop: 8, maxWidth: 380, marginInline: "auto" }}>
                  Thanks {form.name.split(" ")[0]}. We'll reply to <strong style={{ color: "var(--ink)" }}>{form.email}</strong> within 48 hours.
                </p>
                <div style={{ marginTop: 28, display: "inline-flex", gap: 8 }}>
                  <Button variant="ghost" onClick={() => { setSent(false); setForm({ name: "", email: "", phone: "", topic: "Rehab & Performance Coaching", message: "" }); }}>Send another</Button>
                  <Button variant="primary" onClick={() => navigate("services")}>Browse services</Button>
                </div>
              </div>
            ) : (
              <form onSubmit={submit} style={{ display: "flex", flexDirection: "column", gap: 18 }}>
                <h3 className="display-md">Send a message.</h3>
                <div className="grid-2" style={{ gap: 14 }}>
                  <div className={`field ${errors.name ? "error" : ""}`}>
                    <label>Full name</label>
                    <input value={form.name} onChange={(e) => set("name", e.target.value)} placeholder="Your name" />
                    {errors.name && <span className="err">{errors.name}</span>}
                  </div>
                  <div className={`field ${errors.email ? "error" : ""}`}>
                    <label>Email</label>
                    <input value={form.email} onChange={(e) => set("email", e.target.value)} placeholder="you@example.com" />
                    {errors.email && <span className="err">{errors.email}</span>}
                  </div>
                </div>
                <div className="grid-2" style={{ gap: 14 }}>
                  <div className="field">
                    <label>Phone (optional)</label>
                    <input value={form.phone} onChange={(e) => set("phone", e.target.value)} placeholder="+31 ··· ··· ···" />
                  </div>
                  <div className="field">
                    <label>What's this about?</label>
                    <select value={form.topic} onChange={(e) => set("topic", e.target.value)}>
                      <option>Rehab &amp; Performance Coaching</option>
                      <option>Personal Coaching</option>
                      <option>Online Coaching</option>
                      <option>40-Day Reset Challenge</option>
                      <option>Speaking / collaboration</option>
                      <option>Press / media</option>
                      <option>Something else</option>
                    </select>
                  </div>
                </div>
                <div className={`field ${errors.message ? "error" : ""}`}>
                  <label>Message</label>
                  <textarea value={form.message} onChange={(e) => set("message", e.target.value)} placeholder="Tell us about your goal, any pain or injury history, and what you've tried so far…" />
                  {errors.message && <span className="err">{errors.message}</span>}
                </div>
                {submitError && (
                  <div style={{ fontSize: 13, color: "#b00020", background: "rgba(176,0,32,.06)", border: "1px solid rgba(176,0,32,.2)", borderRadius: 8, padding: "10px 12px" }}>
                    {submitError}
                  </div>
                )}
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 4, flexWrap: "wrap", gap: 12 }}>
                  <span style={{ fontSize: 12, color: "var(--muted)", display: "inline-flex", alignItems: "center", gap: 6 }}>
                    <Icon name="clock" size={12} />
                    Reply within 48 hours.
                  </span>
                  <Button type="submit" variant="primary" icon="send" disabled={submitting}>{submitting ? "Sending\u2026" : "Send message"}</Button>
                </div>
              </form>
            )}
          </div>

          {/* Sidebar */}
          <aside style={{ display: "flex", flexDirection: "column", gap: 16 }}>
            <div className="card-accent" style={{ padding: 24, position: "relative", overflow: "hidden" }}>
              <Icon name="calendar" size={20} />
              <h5 className="title-md" style={{ marginTop: 64, fontSize: 18 }}>Book a free intake call</h5>
              <p style={{ fontSize: 13, color: "rgba(38,37,30,.7)", marginTop: 6 }}>30 minutes · honest assessment of fit.</p>
              <a className="btn btn-primary btn-sm" href="https://calendly.com/7araka-coaching/30min" target="_blank" rel="noopener" style={{ marginTop: 14 }}>
                Open calendar <Icon name="arrow-right" size={12} />
              </a>
            </div>

            <div className="card" style={{ padding: 24 }}>
              <h6 className="title-sm">Direct</h6>
              <ul style={{ listStyle: "none", padding: 0, margin: "14px 0 0", display: "flex", flexDirection: "column", gap: 12 }}>
                <li style={{ display: "flex", alignItems: "center", gap: 12 }}>
                  <span style={{ width: 32, height: 32, borderRadius: 8, background: "var(--surface-strong)", display: "grid", placeItems: "center" }}><Icon name="mail" size={14} /></span>
                  <div>
                    <div style={{ fontSize: 11, color: "var(--muted)", textTransform: "uppercase", letterSpacing: 0.5 }}>Email</div>
                    <a href="mailto:info@7araka.co" style={{ fontSize: 14, fontWeight: 500 }}>info@7araka.co</a>
                  </div>
                </li>
                <li style={{ display: "flex", alignItems: "center", gap: 12 }}>
                  <span style={{ width: 32, height: 32, borderRadius: 8, background: "var(--surface-strong)", display: "grid", placeItems: "center" }}><Icon name="phone" size={14} /></span>
                  <div>
                    <div style={{ fontSize: 11, color: "var(--muted)", textTransform: "uppercase", letterSpacing: 0.5 }}>Phone / WhatsApp</div>
                    <a href="tel:+31640030280" style={{ fontSize: 14, fontWeight: 500 }}>+31 6 40 03 02 80</a>
                  </div>
                </li>
                <li style={{ display: "flex", alignItems: "center", gap: 12 }}>
                  <span style={{ width: 32, height: 32, borderRadius: 8, background: "var(--surface-strong)", display: "grid", placeItems: "center" }}><Icon name="map-pin" size={14} /></span>
                  <div>
                    <div style={{ fontSize: 11, color: "var(--muted)", textTransform: "uppercase", letterSpacing: 0.5 }}>Locations</div>
                    <span style={{ fontSize: 14, fontWeight: 500 }}>Studios across Amsterdam & Haarlem</span>
                  </div>
                </li>
              </ul>
            </div>

            <div className="card" style={{ padding: 24 }}>
              <h6 className="title-sm">Hours</h6>
              <ul style={{ listStyle: "none", padding: 0, margin: "14px 0 0", display: "flex", flexDirection: "column", gap: 8, fontSize: 14 }}>
                <li style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--muted)" }}>Mon – Thu</span><span>07:00 – 20:00</span></li>
                <li style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--muted)" }}>Friday</span><span>07:00 – 14:00</span></li>
                <li style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--muted)" }}>Saturday</span><span>09:00 – 13:00</span></li>
                <li style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--muted)" }}>Sunday</span><span>Closed</span></li>
              </ul>
            </div>

            <div className="card-dark" style={{ padding: 24 }}>
              <h6 className="title-sm" style={{ color: "var(--canvas)" }}>Follow the practice</h6>
              <p style={{ color: "rgba(247,247,244,.7)", fontSize: 13, marginTop: 8 }}>
                Daily training, recovery, and discipline lessons on Instagram and TikTok. Long-form on LinkedIn.
              </p>
              <div style={{ display: "flex", gap: 8, marginTop: 14 }}>
                <a href="https://www.instagram.com/coach.bido" target="_blank" rel="noopener" aria-label="Instagram" style={{ width: 36, height: 36, borderRadius: 8, border: "1px solid rgba(255,255,255,.15)", display: "grid", placeItems: "center", color: "var(--canvas)" }}><Icon name="instagram" size={14} /></a>
                <a href="https://www.tiktok.com/@bido.7arakacoaching" target="_blank" rel="noopener" aria-label="TikTok" style={{ width: 36, height: 36, borderRadius: 8, border: "1px solid rgba(255,255,255,.15)", display: "grid", placeItems: "center", color: "var(--canvas)" }}><Icon name="tiktok" size={14} /></a>
                <a href="https://www.linkedin.com/in/bidomohamed" target="_blank" rel="noopener" aria-label="LinkedIn" style={{ width: 36, height: 36, borderRadius: 8, border: "1px solid rgba(255,255,255,.15)", display: "grid", placeItems: "center", color: "var(--canvas)" }}><Icon name="linkedin" size={14} /></a>
              </div>
            </div>
          </aside>
        </div>
      </section>

      {/* ---------- Map / studio band ---------- */}
      <section style={{ padding: "0 0 80px" }}>
        <div className="container-wide">
          <div className="card-image" style={{ aspectRatio: "21 / 8" }}>
            <img src={R('imgStudioWide', "https://images.unsplash.com/photo-1534438327276-14e5300c3a48?auto=format&fit=crop&w=2000&q=80")} alt="" />
            <div className="photo-badge" style={{ top: 20, left: 20 }}>
              <Icon name="map-pin" size={12} />
              Locations · Amsterdam & Haarlem
            </div>
            <div className="photo-badge" style={{ bottom: 20, right: 20 }}>
              <Icon name="users" size={12} />
              Private sessions only · By appointment
            </div>
          </div>
        </div>
      </section>
    </Page>
  );
};

Object.assign(window, { ContactPage });
