/* About + Journal + Contact pages. */

/* ── About ─────────────────────────────────────── */
const FounderCard = ({ f, lang }) => (
  <article style={{ display: "flex", flexDirection: "column", gap: 24 }}>
    {f.photo ? (
      <img src={f.photo} alt={f.name.en}
           style={{ width: "100%", aspectRatio: "4 / 5",
                    objectFit: "cover",
                    objectPosition: f.photoPosition || "center",
                    display: "block",
                    filter: "grayscale(15%)" }} />
    ) : (
      <div style={{
        width: "100%", aspectRatio: "4 / 5",
        background: "var(--color-paper)",
        border: "1px solid var(--color-rule)",
        display: "flex", alignItems: "center", justifyContent: "center",
        color: "var(--color-ink-3)",
        fontFamily: "var(--font-mono)", fontSize: 11,
        letterSpacing: "0.18em", textTransform: "uppercase"
      }}>
        photo · pending
      </div>
    )}
    <div>
      <h3 style={{
        fontFamily: "var(--font-serif)", fontWeight: 400,
        fontSize: 28, lineHeight: 1.15, letterSpacing: "-0.01em",
        margin: "0 0 6px"
      }}>
        {f.name[lang]}
      </h3>
      <div style={{
        fontFamily: "var(--font-sans)", fontSize: 14,
        color: "var(--color-ink-3)", fontStyle: "italic",
        marginBottom: 20
      }}>
        {f.role[lang]}
      </div>
      <p style={{
        fontSize: 15.5, lineHeight: 1.65,
        color: "var(--color-ink-2)", margin: 0
      }}>
        {f.bio[lang]}
      </p>
    </div>
  </article>
);

const PageAbout = () => {
  const { lang } = useT();
  const a = window.MR.about;
  return (
    <main>
      {/* Hero */}
      <section className="mr-section--hero" style={{ borderBottom: "1px solid var(--color-rule)" }}>
        <div className="mr-container">
          <Reveal><span className="eyebrow">{a.kicker[lang]}</span></Reveal>
          <Reveal delay={120} as="h1" style={{
            fontFamily: "var(--font-serif)", fontWeight: 400,
            fontSize: "clamp(40px, 6vw, 84px)", lineHeight: 1.05,
            letterSpacing: "-0.02em", margin: "16px 0 0",
            textWrap: "balance"
          }}>
            {a.hero[lang]}
          </Reveal>
        </div>
      </section>

      {/* Founders */}
      <section className="mr-section" style={{ borderBottom: "1px solid var(--color-rule)" }}>
        <div className="mr-container">
          <Reveal>
            <span className="eyebrow">{a.founders.eyebrow[lang]}</span>
            <h2 style={{
              fontFamily: "var(--font-serif)", fontWeight: 400,
              fontSize: "clamp(32px, 4vw, 56px)", lineHeight: 1.1,
              letterSpacing: "-0.02em", margin: "12px 0 24px",
              textWrap: "balance"
            }}>
              {a.founders.title[lang]}
            </h2>
            <p style={{
              fontFamily: "var(--font-serif)", fontStyle: "italic",
              fontSize: 20, lineHeight: 1.5,
              color: "var(--color-ink-2)",
              maxWidth: 720, margin: "0 0 64px"
            }}>
              {a.founders.intro[lang]}
            </p>
          </Reveal>
          <div className="mr-founders-grid"
               style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 64 }}>
            {a.founders.items.map((f, i) => (
              <Reveal key={i} delay={i * 120}>
                <FounderCard f={f} lang={lang} />
              </Reveal>
            ))}
          </div>
        </div>
      </section>

      {/* Mission */}
      <section className="mr-section">
        <div className="mr-container"
             style={{ display: "grid", gridTemplateColumns: "1fr 2fr", gap: 48 }}>
          <Reveal><span className="eyebrow">{a.mission.eyebrow[lang]}</span></Reveal>
          <Reveal delay={120}>
            <h2 style={{
              fontFamily: "var(--font-serif)", fontWeight: 400,
              fontSize: "clamp(28px, 3.4vw, 44px)",
              lineHeight: 1.15, letterSpacing: "-0.015em",
              margin: "0 0 28px", textWrap: "balance",
              maxWidth: 760
            }}>
              {a.mission.title[lang]}
            </h2>
            <p style={{
              fontSize: 17, lineHeight: 1.7,
              color: "var(--color-ink-2)", margin: 0,
              maxWidth: 700
            }}>
              {a.mission.body[lang]}
            </p>
          </Reveal>
        </div>
      </section>
    </main>
  );
};

window.PageAbout = PageAbout;

/* ── Journal ───────────────────────────────────── */
const PageJournal = () => {
  const { lang } = useT();
  const j = window.MR.journal;
  return (
    <main>
      <section className="mr-section--hero" style={{ borderBottom: "1px solid var(--color-rule)" }}>
        <div className="mr-container">
          <Reveal><span className="eyebrow">{j.kicker[lang]}</span></Reveal>
          <Reveal delay={120} as="h1" style={{
            fontFamily: "var(--font-serif)", fontWeight: 400,
            fontSize: "clamp(40px, 6vw, 72px)", lineHeight: 1.05,
            letterSpacing: "-0.02em", margin: "16px 0 32px"
          }}>
            {j.title[lang]}
          </Reveal>
          <Reveal delay={240} as="p" style={{
            fontFamily: "var(--font-serif)", fontStyle: "italic",
            fontSize: 20, lineHeight: 1.5, color: "var(--color-ink-2)",
            maxWidth: 640, margin: 0
          }}>
            {j.lede[lang]}
          </Reveal>
        </div>
      </section>
      <section className="mr-section">
        <div className="mr-container">
          {j.entries.map((e, i) => (
            <Reveal key={e.num} delay={i * 60} as="article" style={{
              display: "grid", gridTemplateColumns: "180px 1fr",
              gap: 48, padding: "56px 0",
              borderBottom: "1px solid var(--color-rule)"
            }}>
              <div>
                <div style={{ fontFamily: "var(--font-mono)", fontSize: 13,
                              letterSpacing: "0.10em", color: "var(--color-ink)" }}>
                  {e.date}
                </div>
                <div style={{ fontFamily: "var(--font-mono)", fontSize: 11,
                              letterSpacing: "0.16em", color: "var(--color-ink-3)",
                              marginTop: 8 }}>
                  №{e.num}
                </div>
              </div>
              <div>
                <h2 style={{ fontFamily: "var(--font-serif)", fontWeight: 400,
                             fontSize: 30, lineHeight: 1.15, letterSpacing: "-0.01em",
                             margin: "0 0 16px", textWrap: "balance" }}>
                  {lang === "ka" ? e.titleKa : e.titleEn}
                </h2>
                <p style={{ fontSize: 17, lineHeight: 1.65,
                            color: "var(--color-ink-2)", margin: 0,
                            maxWidth: 680 }}>
                  {lang === "ka" ? e.bodyKa : e.bodyEn}
                </p>
              </div>
            </Reveal>
          ))}
        </div>
      </section>
    </main>
  );
};

window.PageJournal = PageJournal;

/* ── Contact ───────────────────────────────────── */

// Web3Forms public access key — paste yours from https://web3forms.com (free, 250/mo).
// Safe to commit: it's a per-form public identifier, not a secret.
const WEB3FORMS_ACCESS_KEY = "1ea9466f-e754-4283-89ba-e536d09e7826";

const ContactForm = ({ lang, copy }) => {
  const [status, setStatus] = React.useState("idle"); // idle | sending | sent | error
  const [subject, setSubject] = React.useState("general");

  const onSubmit = async (e) => {
    e.preventDefault();
    setStatus("sending");
    const data = new FormData(e.target);
    data.append("access_key", WEB3FORMS_ACCESS_KEY);
    data.append("from_name", "Mimino Robotics — contact form");
    try {
      const res = await fetch("https://api.web3forms.com/submit", {
        method: "POST",
        body: data,
      });
      const json = await res.json();
      if (json.success) {
        setStatus("sent");
        e.target.reset();
        setSubject("general");
      } else {
        setStatus("error");
      }
    } catch {
      setStatus("error");
    }
  };

  const labelStyle = {
    display: "block",
    fontFamily: "var(--font-mono)",
    fontSize: 11,
    letterSpacing: "0.14em",
    textTransform: "uppercase",
    color: "var(--color-ink-3)",
    marginBottom: 10,
  };
  const inputStyle = {
    width: "100%",
    background: "transparent",
    border: 0,
    borderBottom: "1px solid var(--color-rule)",
    padding: "10px 0",
    fontSize: 16,
    fontFamily: "var(--font-sans)",
    color: "var(--color-ink)",
    outline: "none",
    transition: "border-color 200ms ease-out",
    boxSizing: "border-box",
  };

  return (
    <form onSubmit={onSubmit} className="mr-contact-form">
      <input type="checkbox" name="botcheck" tabIndex={-1} aria-hidden="true"
             style={{ position: "absolute", left: -9999, opacity: 0 }} />

      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 32 }}>
        <div>
          <label htmlFor="cf-name" style={labelStyle}>{copy.fields.name[lang]}</label>
          <input id="cf-name" name="name" type="text" required style={inputStyle} />
        </div>
        <div>
          <label htmlFor="cf-email" style={labelStyle}>{copy.fields.email[lang]}</label>
          <input id="cf-email" name="email" type="email" required style={inputStyle} />
        </div>
      </div>

      <div style={{ marginTop: 32 }}>
        <label htmlFor="cf-company" style={labelStyle}>{copy.fields.company[lang]}</label>
        <input id="cf-company" name="company" type="text" style={inputStyle} />
      </div>

      <div style={{ marginTop: 32 }}>
        <label htmlFor="cf-subject" style={labelStyle}>{copy.fields.subject[lang]}</label>
        <select id="cf-subject" name="subject" value={subject}
                onChange={(e) => setSubject(e.target.value)}
                style={{ ...inputStyle, appearance: "none",
                         backgroundImage: "linear-gradient(45deg, transparent 50%, var(--color-ink-3) 50%), linear-gradient(135deg, var(--color-ink-3) 50%, transparent 50%)",
                         backgroundPosition: "calc(100% - 14px) 18px, calc(100% - 9px) 18px",
                         backgroundSize: "5px 5px, 5px 5px",
                         backgroundRepeat: "no-repeat",
                         cursor: "pointer", paddingRight: 32 }}>
          {copy.subjects.map((s) => (
            <option key={s.value} value={s.value}>{s[lang]}</option>
          ))}
        </select>
      </div>

      <div style={{ marginTop: 32 }}>
        <label htmlFor="cf-message" style={labelStyle}>{copy.fields.message[lang]}</label>
        <textarea id="cf-message" name="message" required rows={5}
                  style={{ ...inputStyle, resize: "vertical", lineHeight: 1.55,
                           paddingTop: 12, minHeight: 140 }} />
      </div>

      <div style={{ marginTop: 40, display: "flex", alignItems: "center",
                    gap: 24, flexWrap: "wrap" }}>
        <button type="submit" disabled={status === "sending"}
                style={{
                  background: "var(--color-ink)",
                  color: "var(--color-bg)",
                  border: 0,
                  padding: "14px 32px",
                  fontSize: 14,
                  letterSpacing: "0.04em",
                  fontFamily: "var(--font-sans)",
                  cursor: status === "sending" ? "wait" : "pointer",
                  opacity: status === "sending" ? 0.6 : 1,
                  transition: "background 240ms ease-out, opacity 200ms ease-out",
                }}>
          {status === "sending" ? copy.sending[lang] : copy.submit[lang]}
        </button>
        {status === "sent" && (
          <span style={{ fontSize: 14, color: "var(--color-ink-2)" }}>
            {copy.success[lang]}
          </span>
        )}
        {status === "error" && (
          <span style={{ fontSize: 14, color: "var(--color-accent)" }}>
            {copy.error[lang]}
          </span>
        )}
      </div>
    </form>
  );
};

const PageContact = () => {
  const { lang } = useT();
  const c = window.MR.contact;
  return (
    <main>
      <section className="mr-section--hero" style={{ borderBottom: "1px solid var(--color-rule)" }}>
        <div className="mr-container">
          <Reveal><span className="eyebrow">{c.kicker[lang]}</span></Reveal>
          <Reveal delay={120} as="h1" style={{
            fontFamily: "var(--font-serif)", fontWeight: 400,
            fontSize: "clamp(40px, 6vw, 84px)", lineHeight: 1.0,
            letterSpacing: "-0.02em", margin: "16px 0 32px"
          }}>
            {c.title[lang]}
          </Reveal>
        </div>
      </section>

      <section className="mr-section">
        <div className="mr-container">
          <Reveal>
            <span className="eyebrow" style={{ display: "block", marginBottom: 32 }}>
              {c.form.eyebrow[lang]}
            </span>
            <ContactForm lang={lang} copy={c.form} />
          </Reveal>
        </div>
      </section>

      <section className="mr-section--lg" style={{ textAlign: "center" }}>
        <p style={{ fontFamily: "var(--font-serif)", fontStyle: "italic",
                    fontSize: 26, color: "var(--color-ink-2)",
                    margin: 0, display: "inline-flex", alignItems: "center", gap: 16 }}>
          {c.sign[lang]} <FlagGE w={22} h={14}/>
        </p>
      </section>
    </main>
  );
};

window.PageContact = PageContact;

/* ── Coming Soon ───────────────────────────────── */
const PageComingSoon = () => {
  const { lang } = useT();
  const c = window.MR.comingSoon;
  return (
    <main>
      <section className="mr-section--lg"
               style={{ minHeight: "55vh", display: "flex", alignItems: "center" }}>
        <div className="mr-container" style={{ textAlign: "center" }}>
          <Reveal><span className="eyebrow">{c.kicker[lang]}</span></Reveal>
          <Reveal delay={120} as="h1" style={{
            fontFamily: "var(--font-serif)", fontWeight: 400,
            fontSize: "clamp(48px, 7vw, 96px)", lineHeight: 1.05,
            letterSpacing: "-0.02em", margin: "16px 0 24px",
            textWrap: "balance"
          }}>
            {c.title[lang]}
          </Reveal>
          <Reveal delay={240} as="p" style={{
            fontFamily: "var(--font-serif)", fontStyle: "italic",
            fontSize: 20, lineHeight: 1.5, color: "var(--color-ink-2)",
            maxWidth: 560, margin: "0 auto 40px"
          }}>
            {c.body[lang]}
          </Reveal>
          <Reveal delay={360}>
            <RLink to="/" className="mr-link">{c.back[lang]}</RLink>
          </Reveal>
        </div>
      </section>
    </main>
  );
};

window.PageComingSoon = PageComingSoon;

/* ── 404 Not Found ─────────────────────────────── */
const PageNotFound = () => {
  const { lang } = useT();
  return (
    <main>
      <section className="mr-section--hero" style={{ borderBottom: "1px solid var(--color-rule)" }}>
        <div className="mr-container">
          <Reveal><span className="eyebrow">404</span></Reveal>
          <Reveal delay={120} as="h1" style={{
            fontFamily: "var(--font-serif)", fontWeight: 400,
            fontSize: "clamp(40px, 6vw, 72px)", lineHeight: 1.05,
            letterSpacing: "-0.02em", margin: "16px 0 28px", maxWidth: 720
          }}>
            {lang === "ka" ? "გვერდი ვერ მოიძებნა." : "Page not found."}
          </Reveal>
          <Reveal delay={240}>
            <RLink to="/" className="mr-link">
              {lang === "ka" ? "← მთავარ გვერდზე დაბრუნება" : "← Back to home"}
            </RLink>
          </Reveal>
        </div>
      </section>
    </main>
  );
};

window.PageNotFound = PageNotFound;
