// ============================================================
// Husky Paths — SubmitFlow modal + ContactModal
// ============================================================

// ── Submit flow: form → verifying → success ─────────────────
const SubmitFlow = ({ isOpen, onClose, onSubmit, backendReady, authSession, onAuthChanged, backendStatus, tweaks, initialProfile = null, onSubmitForReview }) => {
  const [step, setStep] = useState("form"); // form | auth | verifying | success
  const [error, setError] = useState("");
  const [otp, setOtp] = useState("");
  const [authBusy, setAuthBusy] = useState(false);
  const [authResendSeconds, setAuthResendSeconds] = useState(0);
  const [shareCopied, setShareCopied] = useState(false);
  const [data, setData] = useState({
    name: "", email: "", major: "", year: "2025",
    pathType: "Industry", orgName: "", roleDetail: "", city: "",
    openToConnect: true, linkedin: "", wechat: "", instagram: "",
    note: "",
  });
  const [consent, setConsent] = useState(false);
  // 验证后若检测到该账号已有档案，存这里 → 防止空白覆盖
  const [existingProfile, setExistingProfile] = useState(null);
  // 编辑模式（已有路径）默认视为已同意过
  useEffect(() => { if (initialProfile) setConsent(true); }, [initialProfile?.id]);
  const verifySteps = [
    { label: "Verifying @uw.edu", zh: "验证 @uw.edu" },
    { label: "Checking duplicates",   zh: "查重中" },
    { label: "Pinning to atlas",      zh: "标记地图" },
  ];
  const [verifyIdx, setVerifyIdx] = useState(0);
  const mode = (initialProfile || existingProfile) ? "edit" : "create";

  const emptyData = {
    name: "", email: "", major: "", year: "2025",
    pathType: "Industry", orgName: "", roleDetail: "", city: "",
    openToConnect: true, linkedin: "", wechat: "", instagram: "",
    note: "",
  };

  const dataFromProfile = (profile) => profile ? {
    name: profile.name === "Anonymous" ? "" : profile.name || "",
    email: profile.email || authSession?.user?.email || "",
    major: profile.major || "",
    year: profile.year || "2025",
    pathType: profile.pathType || "Industry",
    orgName: profile.orgName || "",
    roleDetail: profile.roleDetail || "",
    city: profile.city || "",
    openToConnect: !!profile.openToConnect,
    linkedin: profile.linkedin || "",
    wechat: profile.wechat || "",
    instagram: profile.instagram || "",
    note: profile.note || "",
  } : emptyData;

  useEffect(() => {
    if (isOpen && !existingProfile) {
      // 已带出旧档案时不再覆盖（afterVerified 已合并预填）
      setData(dataFromProfile(initialProfile));
    }
  }, [isOpen, initialProfile?.id, authSession?.user?.email, existingProfile]);

  useEffect(() => {
    if (!isOpen) {
      // reset
      setTimeout(() => {
        setStep("form"); setError(""); setVerifyIdx(0); setOtp(""); setAuthBusy(false); setAuthResendSeconds(0); setShareCopied(false);
        setData(emptyData); setConsent(false); setExistingProfile(null);
      }, 300);
    }
  }, [isOpen]);

  useEffect(() => {
    if (authResendSeconds <= 0) return;
    const t = setTimeout(() => setAuthResendSeconds((s) => Math.max(0, s - 1)), 1000);
    return () => clearTimeout(t);
  }, [authResendSeconds]);

  useEffect(() => {
    if (step !== "verifying") return;
    let cancelled = false;
    let i = 0;
    setVerifyIdx(0);
    const t = setInterval(() => {
      i++;
      if (i >= verifySteps.length) {
        clearInterval(t);
        const newProfile = {
          ...data,
          id: `u-${Date.now()}`,
          name: data.name.trim() || "Anonymous",
        };
        Promise.resolve(onSubmit(newProfile))
          .then(() => {
            if (!cancelled) setStep("success");
          })
          .catch((err) => {
            if (!cancelled) {
              setError(err.message || "Could not save your path.");
              setStep("form");
            }
          });
        return;
      }
      setVerifyIdx(i);
    }, 700);
    return () => {
      cancelled = true;
      clearInterval(t);
    };
  }, [step]);

  if (!isOpen) return null;

  const normalizedEmail = data.email.trim().toLowerCase();
  const sessionEmail = authSession?.user?.email?.toLowerCase() || "";

  const sendVerificationEmail = async () => {
    setAuthBusy(true);
    setError("");
    try {
      await window.HuskyBackend.sendLoginLink(normalizedEmail);
      setAuthResendSeconds(60);
      setStep("auth");
    } catch (err) {
      setError(err.message || "Could not send verification email.");
    } finally {
      setAuthBusy(false);
    }
  };

  // 老用户:不必先填表，输入 @uw.edu 邮箱即可验证登录，验证后自动带出旧档案
  const startLoginToEdit = async () => {
    setError("");
    if (!/^[^@]+@uw\.edu$/.test(normalizedEmail)) {
      setError("Enter your @uw.edu email above first, then sign in to edit. · 先在上方填 @uw.edu 邮箱再登录。");
      return;
    }
    await sendVerificationEmail();
  };

  // 安全网:旧档案打底，用户本次已填的非空字段覆盖 → 防止空白覆盖、保留联系方式
  const mergeFormData = (existing, typed) => {
    const merged = {
      name: existing.name === "Anonymous" ? "" : (existing.name || ""),
      email: existing.email || normalizedEmail || "",
      major: existing.major || "",
      year: existing.year || "2025",
      pathType: existing.pathType || "Industry",
      orgName: existing.orgName || "",
      roleDetail: existing.roleDetail || "",
      city: existing.city || "",
      openToConnect: !!existing.openToConnect,
      linkedin: existing.linkedin || "",
      wechat: existing.wechat || "",
      instagram: existing.instagram || "",
      note: existing.note || "",
    };
    ["name", "major", "orgName", "roleDetail", "city", "note", "linkedin", "wechat", "instagram"].forEach((k) => {
      if (typed[k] && String(typed[k]).trim()) merged[k] = typed[k];
    });
    if (typed.year && typed.year !== "2025") merged.year = typed.year;
    if (typed.pathType) merged.pathType = typed.pathType;
    if (typed.openToConnect) merged.openToConnect = true;
    return merged;
  };

  // 验证成功后：若该账号已有档案且非编辑模式，预填旧档案回到表单（不直接覆盖提交）
  const afterVerified = async (session) => {
    const typedSnapshot = data; // 在 onAuthChanged 触发重渲染前先抓住用户已填内容
    onAuthChanged?.(session);
    if (!initialProfile) {
      let existing = null;
      try { existing = await window.HuskyBackend.getMyProfile(); } catch (_e) { existing = null; }
      if (existing) {
        setExistingProfile(existing);
        setData(mergeFormData(existing, typedSnapshot));
        setConsent(true);
        setError("");
        setStep("form");
        return;
      }
    }
    // 新用户：恢复用户填的内容（防止登录引发的预填 effect 把它清空），再进入提交
    setData(typedSnapshot);
    setStep("verifying");
  };

  const handleVerifyOtp = async () => {
    setAuthBusy(true);
    setError("");
    try {
      const session = await window.HuskyBackend.verifyOtp(normalizedEmail, otp);
      await afterVerified(session);
    } catch (err) {
      setError(err.message || "Verification failed.");
    } finally {
      setAuthBusy(false);
    }
  };

  const handleMagicLinkReturn = async () => {
    setAuthBusy(true);
    setError("");
    try {
      const session = await window.HuskyBackend.getSession();
      if (!session?.user) {
        setError("No verified session found yet. Open the email link or paste the code first.");
        return;
      }
      if (session.user.email.toLowerCase() !== normalizedEmail) {
        setError(`You are signed in as ${session.user.email}. Use that email in the form.`);
        return;
      }
      await afterVerified(session);
    } catch (err) {
      setError(err.message || "Could not confirm your session.");
    } finally {
      setAuthBusy(false);
    }
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    setError("");
    // 非 @uw.edu 邮箱 → 不再报错,转入审核队列(带着已填内容,邮箱作为身份线索)
    if (normalizedEmail && !/^[^@]+@uw\.edu$/.test(normalizedEmail) && onSubmitForReview) {
      onSubmitForReview({
        name: data.name, major: data.major, year: data.year, pathType: data.pathType,
        orgName: data.orgName, roleDetail: data.roleDetail, city: data.city, note: data.note,
        contactHint: normalizedEmail,
      });
      return;
    }
    if (!/^[^@]+@uw\.edu$/.test(normalizedEmail)) { setError(t("Please use a valid @uw.edu email address.", "请填写有效的 @uw.edu 邮箱。")); return; }
    if (!data.major || !data.orgName || !data.city) { setError(t("Major, company/school, and city are required.", "请填写专业、机构和城市。")); return; }
    if (!consent) { setError(t("Please confirm the submission terms before continuing.", "请先勾选下方确认条款。")); return; }
    if (backendReady && !authSession) {
      await sendVerificationEmail();
      return;
    }
    if (backendReady && sessionEmail !== normalizedEmail) {
      setError(`You are signed in as ${sessionEmail}. Please use that UW email in the form.`);
      return;
    }
    setStep("verifying");
  };

  const handleInviteShare = async () => {
    const cityForShare = (data.city || "").trim();
    const shareText = cityForShare
      ? `I just pinned my path to ${cityForShare} on Husky Paths — a student-built map of where UW grads land. Add yours so the next class can find people like us.`
      : "I just added my post-grad path to Husky Paths — a student-built map of where UW grads land. Add yours so the next class can find people like us.";
    const shareData = {
      title: "Husky Paths · UW alumni atlas",
      text: shareText,
      url: window.location.origin,
    };
    try {
      if (navigator.share) {
        await navigator.share(shareData);
      } else {
        await navigator.clipboard?.writeText(`${shareData.text} ${shareData.url}`);
        setShareCopied(true);
        setTimeout(() => setShareCopied(false), 1800);
      }
    } catch (_err) {
      await navigator.clipboard?.writeText(`${shareData.text} ${shareData.url}`);
      setShareCopied(true);
      setTimeout(() => setShareCopied(false), 1800);
    }
  };

  return (
    <div className="hp-modal-scrim" onClick={onClose}>
      <div className="hp-modal" onClick={(e) => e.stopPropagation()}>
        {step === "form" && (
          <>
            <div className="hp-modal-head">
              <div>
                <Overline en="Add a path" zh="添加路径" color="var(--accent-coral)" />
                <h2 className="hp-modal-title">{mode === "edit" ? t("Update your path.", "更新你的路径。") : t("Tell us where you're headed.", "告诉我们你的去向。")}</h2>
                <p className="hp-italic" style={{ marginTop: 6 }}>
                  {mode === "edit"
                    ? t("Keep your destination and contact preferences current.", "让你的去向和联系偏好保持最新。")
                    : t("Show the next class what's possible. Anonymous submissions are fine — just leave the name blank.", "让下一届看到更多可能。匿名提交也没问题——留空姓名即可。")}
                </p>
              </div>
              <button className="hp-close-btn" onClick={onClose}><HPIcons.X /></button>
            </div>

            <form onSubmit={handleSubmit} className="hp-form">
              {error && <div className="hp-form-error">{error}</div>}
              {existingProfile && (
                <div className="hp-form-notice">
                  ✦ {t("We found your existing path — review and update below. Your contact info is kept unless you change it.",
                       "已带出你之前填的资料,核对后更新即可;联系方式会保留,除非你修改。")}
                </div>
              )}
              {!authSession && !existingProfile && mode !== "edit" && (
                <div className="hp-form-hint">
                  {t("Already added your path? ", "已经填过了?")}
                  <button type="button" className="hp-link-btn" onClick={startLoginToEdit}>
                    {t("Sign in to edit", "登录修改")}
                  </button>
                  {onSubmitForReview && (
                    <>
                      <br/>
                      {t("Not a UW email, or graduated & lost yours? ", "不是 UW 邮箱、或毕业后邮箱失效了?")}
                      <button type="button" className="hp-link-btn" onClick={onSubmitForReview}>
                        {t("Submit for review", "提交审核")}
                      </button>
                    </>
                  )}
                </div>
              )}

              <div className="hp-form-grid">
                <div className="hp-field">
                  <label>{t("Name", "姓名")} <span className="hp-field-meta">{t("optional", "可选")}</span></label>
                  <input value={data.name} onChange={(e) => setData({ ...data, name: e.target.value })} placeholder={t("Leave blank for anon", "留空即匿名")}/>
                </div>
                <div className="hp-field">
                  <label>{t("Email", "邮箱")} <span className="hp-req">*</span></label>
                  <input type="email" value={data.email} onChange={(e) => setData({ ...data, email: e.target.value })} placeholder={t("netid@uw.edu or any email", "netid@uw.edu 或任意邮箱")} required/>
                </div>
              </div>
              <p className="hp-field-note">
                {t("Have a @uw.edu? You'll verify and appear on the map instantly. Any other email is welcome too — your entry just goes through a quick manual review before it shows.",
                   "有 @uw.edu?验证后立即上墙。也欢迎用其他邮箱——你的资料会先经过一次快速人工审核再显示。")}
              </p>

              <div className="hp-form-grid">
                <div className="hp-field">
                  <label>{t("Major", "专业")} <span className="hp-req">*</span></label>
                  <input value={data.major} onChange={(e) => setData({ ...data, major: e.target.value })} placeholder={t("e.g. Computer Science", "如:计算机科学")} required/>
                </div>
                <div className="hp-field">
                  <label>{t("Grad year", "毕业年份")}</label>
                  <select value={data.year} onChange={(e) => setData({ ...data, year: e.target.value })}>
                    {[2026,2025,2024,2023,2022,2021,2020,2019,2018,2017,2016].map((y) => <option key={y}>{y}</option>)}
                  </select>
                </div>
              </div>

              <div className="hp-field">
                <label>{t("Next step", "下一步去向")} <span className="hp-req">*</span></label>
                <div className="hp-seg">
                  {PATH_TYPES.map((pt) => (
                    <button
                      key={pt.id} type="button"
                      className={`hp-seg-btn ${data.pathType === pt.id ? "is-active" : ""}`}
                      onClick={() => setData({ ...data, pathType: pt.id })}
                    >
                      <span className="hp-path-glyph">{pt.glyph}</span>
                      <span>{t(pt.en, pt.zh)}</span>
                    </button>
                  ))}
                </div>
              </div>

              <div className="hp-form-grid">
                <div className="hp-field">
                  <label>{t("Company / School", "公司 / 学校")} <span className="hp-req">*</span></label>
                  <input value={data.orgName} onChange={(e) => setData({ ...data, orgName: e.target.value })} placeholder={t("Where to?", "去了哪里?")} required/>
                </div>
                <div className="hp-field">
                  <label>{t("City", "城市")} <span className="hp-req">*</span></label>
                  <select value={data.city} onChange={(e) => setData({ ...data, city: e.target.value })} required>
                    <option value="" disabled>{t("Select a city", "选择城市")}</option>
                    {REGIONS.map((r) => (
                      <optgroup key={r} label={r}>
                        {CITIES.filter((c) => c.region === r).sort((a, b) => a.name.localeCompare(b.name)).map((c) => (
                          <option key={c.name} value={c.name}>{c.name}{c.zh ? ` · ${c.zh}` : ""}</option>
                        ))}
                      </optgroup>
                    ))}
                  </select>
                </div>
              </div>

              <div className="hp-field">
                <label>
                  {data.pathType === "Grad School" ? t("Program", "项目")
                    : data.pathType === "Startup" ? t("Role", "角色")
                    : data.pathType === "Gap Year" ? t("Focus", "方向")
                    : t("Position", "职位")}
                  {" "}<span className="hp-field-meta">{t("optional", "可选")}</span>
                </label>
                <input
                  value={data.roleDetail}
                  onChange={(e) => setData({ ...data, roleDetail: e.target.value })}
                  placeholder={
                    data.pathType === "Grad School" ? t("e.g. MS in Computer Science", "如:计算机硕士方向")
                      : data.pathType === "Startup" ? t("e.g. Founding Engineer", "如:创始团队角色")
                      : data.pathType === "Gap Year" ? t("e.g. Traveling, research, applying", "如:旅行、科研、申请中")
                      : t("e.g. Software Engineer", "如:软件工程师")
                  }
                />
              </div>

              <div className="hp-field">
                <label>{t("A line for the next class", "给下一届的一句话")} <span className="hp-field-meta">{t("optional", "可选")}</span></label>
                <textarea
                  value={data.note}
                  onChange={(e) => setData({ ...data, note: e.target.value })}
                  rows={2}
                  placeholder={t("What you wish you'd known. Where to grab coffee. The honest version.", "你当初最希望知道的事;哪里能喝杯好咖啡;真实的版本。")}
                />
              </div>

              <label className="hp-toggle">
                <input type="checkbox" checked={data.openToConnect} onChange={(e) => setData({ ...data, openToConnect: e.target.checked })} />
                <span className="hp-toggle-track"><span className="hp-toggle-thumb" /></span>
                <span className="hp-toggle-body">
                  <strong>{t("Open to connect", "开放联系")}</strong>
                  <span>{t("Only verified @uw.edu users can see your email or handles. Off means destination only.", "仅通过 @uw.edu 验证的用户能看到你的邮箱或账号。关闭则只显示去向。")}</span>
                </span>
              </label>

              {data.openToConnect && (
                <div className="hp-social-block">
                  <Overline en="Social profiles" zh="社交资料" color="var(--accent-coral)" />
                  <div className="hp-social-row">
                    <HPIcons.Linkedin />
                    <input value={data.linkedin} onChange={(e) => setData({ ...data, linkedin: e.target.value })} placeholder="linkedin.com/in/your-handle"/>
                  </div>
                  <div className="hp-social-row">
                    <HPIcons.WeChat />
                    <input value={data.wechat} onChange={(e) => setData({ ...data, wechat: e.target.value })} placeholder="WeChat ID · 微信号"/>
                  </div>
                  <div className="hp-social-row">
                    <HPIcons.Instagram />
                    <input value={data.instagram} onChange={(e) => setData({ ...data, instagram: e.target.value })} placeholder="@your.handle"/>
                  </div>
                </div>
              )}

              <label className={`hp-consent ${error && !consent ? "is-error" : ""}`}>
                <input
                  type="checkbox"
                  checked={consent}
                  onChange={(e) => setConsent(e.target.checked)}
                />
                <span>
                  <strong>{t("I confirm and agree", "我确认并同意")}</strong>
                  {t(
                    "My submission is voluntary and self-reported. I understand Husky Paths is a student-built project, not an official UW service, and that destination details (without my contact info) will be publicly visible on the map. I will only enable contact sharing for friendly one-to-one outreach.",
                    "本提交为我本人自愿、自行填报。我理解 Husky Paths 是学生自建项目、非 UW 官方服务,我的去向信息(不含联系方式)将在地图上公开显示。我仅会为友善的一对一交流开放联系方式。"
                  )}
                </span>
              </label>

              <div className="hp-form-foot">
                <button type="button" className="hp-btn-ghost" onClick={onClose}>{t("Cancel", "取消")}</button>
                <button type="submit" className="hp-btn-primary" disabled={!consent}>
                  {mode === "edit" ? t("Update my path", "更新我的路径") : t("Submit my path", "提交我的路径")} <span className="hp-btn-arrow">→</span>
                </button>
              </div>
            </form>
          </>
        )}

        {step === "auth" && (
          <>
            <div className="hp-modal-head">
              <div>
                <Overline en="UW verification" zh="UW 邮箱验证" color="var(--accent-coral)" />
                <h2 className="hp-modal-title">{t("Enter your UW email code.", "输入 UW 邮箱验证码。")}</h2>
                <p className="hp-italic" style={{ marginTop: 6 }}>
                  {window.__HP_LANG === "zh"
                    ? <>我们已向 {normalizedEmail} 发送 6 位验证码。请复制验证码粘到下方,<strong>不要点邮件里的链接</strong>(点链接会丢掉你填的内容),直接用验证码即可。</>
                    : <>We sent a 6-digit code to {normalizedEmail}. <strong>Copy the code and paste it below</strong> — don't click the link in the email, just use the code so you don't lose your form.</>}
                </p>
              </div>
              <button className="hp-close-btn" onClick={onClose}><HPIcons.X /></button>
            </div>

            <div className="hp-form">
              {error && <div className="hp-form-error">{error}</div>}
              {backendStatus && <div className="hp-form-error" style={{ borderColor: "rgba(75,46,131,.2)", color: "var(--fg-muted)" }}>{backendStatus}</div>}

              <div className="hp-field">
                <label>{t("Verification code", "验证码")} <span className="hp-field-meta">{t("from your UW email", "来自你的 UW 邮箱")}</span></label>
                <input
                  value={otp}
                  onChange={(e) => setOtp(e.target.value)}
                  placeholder={t("6-digit code", "6 位验证码")}
                  inputMode="numeric"
                  autoComplete="one-time-code"
                />
              </div>

              {onSubmitForReview && (
                <div className="hp-form-hint">
                  {t("Code never arrives — @uw.edu deactivated? ", "收不到验证码、@uw.edu 已注销?")}
                  <button type="button" className="hp-link-btn" onClick={onSubmitForReview}>
                    {t("Submit for review instead", "改走审核提交")}
                  </button>
                </div>
              )}

              <div className="hp-form-foot">
                <button type="button" className="hp-btn-ghost" onClick={() => setStep("form")} disabled={authBusy}>{t("Back", "返回")}</button>
                <button type="button" className="hp-btn-ghost" onClick={sendVerificationEmail} disabled={authBusy || authResendSeconds > 0}>
                  {authResendSeconds > 0 ? t(`Resend ${authResendSeconds}s`, `重发 ${authResendSeconds}s`) : t("Resend", "重发")}
                </button>
                <button type="button" className="hp-btn-primary" onClick={handleVerifyOtp} disabled={authBusy || !otp.trim()}>
                  {t("Verify code", "验证")} <span className="hp-btn-arrow">→</span>
                </button>
              </div>
            </div>
          </>
        )}

        {step === "verifying" && (
          <div className="hp-modal-state">
            <div className="hp-verify-mark">
              <HPIcons.ScanMark size={64} color="var(--accent-coral)" />
              <div className="hp-verify-pulse" />
              <div className="hp-verify-pulse hp-verify-pulse-2" />
            </div>
            <Overline en="Verifying" zh="正在验证" color="var(--accent-coral)" />
            <h3 className="hp-modal-title">{t("Pinning you to the atlas.", "正在把你标记到地图。")}</h3>
            <div className="hp-verify-steps">
              {verifySteps.map((s, i) => (
                <div key={i} className={`hp-verify-step ${i < verifyIdx ? "is-done" : ""} ${i === verifyIdx ? "is-active" : ""}`}>
                  <span className="hp-verify-step-mark">
                    {i < verifyIdx ? "✓" : i === verifyIdx ? <span className="hp-verify-spin">○</span> : "·"}
                  </span>
                  <span>{t(s.label, s.zh)}</span>
                </div>
              ))}
            </div>
          </div>
        )}

        {step === "success" && (
          <div className="hp-modal-state">
            <div className="hp-success-mark">
              <HPIcons.ScanMark size={72} color="var(--brand-primary)" />
              <div className="hp-success-sparkle">✦</div>
            </div>
            <Overline en={mode === "edit" ? "Updated" : "Submitted"} zh={mode === "edit" ? "已更新" : "已提交"} color="var(--brand-primary)" />
            <h3 className="hp-modal-title">{mode === "edit" ? t("Your path is current.", "你的路径已更新。") : t("You're on the map.", "你已在地图上了。")}</h3>
            <p className="hp-italic" style={{ maxWidth: 360, textAlign: "center", marginTop: 8 }}>
              {mode === "edit"
                ? t("We saved the latest version of your path.", "已保存你路径的最新版本。")
                : t(`We pinned your path to ${data.city || "the atlas"}.`, `已把你的路径标记到 ${data.city || "地图"}。`)}
              {data.openToConnect
                ? t(" Future Huskies can now reach out to you directly.", " 未来的 Huskies 现在可以直接联系你了。")
                : t(" Your contact info stays private — only the destination is shared.", " 你的联系方式保持私密——只公开去向。")}
            </p>
            <div className="hp-success-actions">
              <button className="hp-btn-ghost" onClick={handleInviteShare}>
                {shareCopied ? t("Copied invite", "已复制邀请") : t("Invite another Husky", "邀请另一位 Husky")}
              </button>
              <button className="hp-btn-primary" onClick={onClose}>
                {t("Back to the map", "回到地图")} <span className="hp-btn-arrow">→</span>
              </button>
            </div>
          </div>
        )}
      </div>
    </div>
  );
};

// ============================================================
// VerifyAccessModal — UW gate before viewing contact details
// ============================================================
const VerifyAccessModal = ({ isOpen, onClose, onVerified, backendStatus, tweaks, title, subtitle, overline }) => {
  const [email, setEmail] = useState("");
  const [otp, setOtp] = useState("");
  const [error, setError] = useState("");
  const [busy, setBusy] = useState(false);
  const [sentTo, setSentTo] = useState("");
  const [resendSeconds, setResendSeconds] = useState(0);

  useEffect(() => {
    if (!isOpen) {
      setTimeout(() => {
        setEmail(""); setOtp(""); setError(""); setBusy(false); setSentTo(""); setResendSeconds(0);
      }, 250);
    }
  }, [isOpen]);

  useEffect(() => {
    if (resendSeconds <= 0) return;
    const t = setTimeout(() => setResendSeconds((s) => Math.max(0, s - 1)), 1000);
    return () => clearTimeout(t);
  }, [resendSeconds]);

  if (!isOpen) return null;

  const normalizedEmail = email.trim().toLowerCase();

  const sendCode = async () => {
    setBusy(true);
    setError("");
    try {
      const sent = await window.HuskyBackend.sendLoginLink(normalizedEmail);
      setSentTo(sent);
      setResendSeconds(60);
    } catch (err) {
      setError(err.message || "Could not send verification code.");
    } finally {
      setBusy(false);
    }
  };

  const verifyCode = async () => {
    setBusy(true);
    setError("");
    try {
      const session = await window.HuskyBackend.verifyOtp(sentTo || normalizedEmail, otp);
      onVerified?.(session);
    } catch (err) {
      setError(err.message || "Verification failed.");
    } finally {
      setBusy(false);
    }
  };

  return (
    <div className="hp-modal-scrim" onClick={onClose}>
      <div className="hp-modal" onClick={(e) => e.stopPropagation()}>
        <div className="hp-modal-head">
          <div>
            <Overline en={overline || "UW only"} zh={overline ? "" : "仅限 UW"} color="var(--accent-coral)" />
            <h2 className="hp-modal-title">{title || t("Verify to connect.", "验证后可联系。")}</h2>
            <p className="hp-italic" style={{ marginTop: 6 }}>
              {subtitle || t("Destination pins are public. Contact details are only visible after a verified @uw.edu sign-in.",
                 "去向图钉公开可见。联系方式仅对通过 @uw.edu 验证登录的用户开放。")}
            </p>
          </div>
          <button className="hp-close-btn" onClick={onClose}><HPIcons.X /></button>
        </div>

        <div className="hp-form">
          {error && <div className="hp-form-error">{error}</div>}
          {backendStatus && <div className="hp-form-error" style={{ borderColor: "rgba(75,46,131,.2)", color: "var(--fg-muted)" }}>{backendStatus}</div>}

          <div className="hp-field">
            <label>{t("UW Email", "UW 邮箱")} <span className="hp-req">*</span></label>
            <input
              type="email"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              placeholder="netid@uw.edu"
              disabled={busy || !!sentTo}
            />
          </div>

          {sentTo && (
            <div className="hp-field">
              <label>{t("Verification code", "验证码")} <span className="hp-field-meta">{t(`sent to ${sentTo}`, `已发送至 ${sentTo}`)}</span></label>
              <input
                value={otp}
                onChange={(e) => setOtp(e.target.value)}
                placeholder={t("6-digit code", "6 位验证码")}
                inputMode="numeric"
                autoComplete="one-time-code"
              />
              <span className="hp-field-meta" style={{ marginTop: 4, display: "block" }}>
                {t("Use the 6-digit code — don't click the link in the email.", "用 6 位验证码——别点邮件里的链接。")}
              </span>
            </div>
          )}

          <div className="hp-form-foot">
            <button type="button" className="hp-btn-ghost" onClick={onClose} disabled={busy}>{t("Cancel", "取消")}</button>
            {sentTo ? (
              <>
                <button type="button" className="hp-btn-ghost" onClick={sendCode} disabled={busy || resendSeconds > 0}>
                  {resendSeconds > 0 ? t(`Resend ${resendSeconds}s`, `重发 ${resendSeconds}s`) : t("Resend", "重发")}
                </button>
                <button type="button" className="hp-btn-primary" onClick={verifyCode} disabled={busy || !otp.trim()}>
                  {t("Verify code", "验证")} <span className="hp-btn-arrow">→</span>
                </button>
              </>
            ) : (
              <button type="button" className="hp-btn-primary" onClick={sendCode} disabled={busy || !normalizedEmail}>
                {t("Send code", "发送验证码")} <span className="hp-btn-arrow">→</span>
              </button>
            )}
          </div>
        </div>
      </div>
    </div>
  );
};

// ============================================================
// ContactModal — social card
// ============================================================
const ContactModal = ({ profile, onClose, tweaks }) => {
  const [copied, setCopied] = useState(null);

  if (!profile) return null;
  const isAnon = !profile.name || profile.name === "Anonymous";
  const initials = isAnon ? "?" : profile.name.split(" ").map((n) => n[0]).join("").slice(0, 2);

  const copy = (text, key) => {
    navigator.clipboard?.writeText(text);
    setCopied(key);
    setTimeout(() => setCopied(null), 1800);
  };

  return (
    <div className="hp-modal-scrim" onClick={onClose}>
      <div className="hp-contact-card" onClick={(e) => e.stopPropagation()}>
        <button className="hp-close-btn hp-contact-close" onClick={onClose}><HPIcons.X /></button>

        {/* Header band */}
        <div className="hp-contact-band">
          <Overline en="Contact card" zh="名片" color="var(--bg-canvas)" />
        </div>

        <div className="hp-contact-body">
          <div className="hp-contact-avatar">{initials}</div>
          <h3 className="hp-contact-name">{profile.name}</h3>
          <div className="hp-contact-role">
            <PathTypeGlyph type={profile.pathType} />
            <span>{profile.orgName}</span>
            <span className="hp-contact-dot">·</span>
            <span>{profile.city}</span>
          </div>
          <div className="hp-contact-meta">
            {profile.major} <span className="hp-contact-dot">·</span> {t(`Class of '${profile.year.slice(-2)}`, `${profile.year}届`)}
          </div>

          {profile.note && (
            <blockquote className="hp-contact-quote">
              "{profile.note}"
            </blockquote>
          )}

          <div className="hp-contact-divider">
            <span>{t("Reach out", "联系方式")}</span>
          </div>

          <div className="hp-contact-links">
            {profile.linkedin && (
              <a className="hp-contact-link" href={`https://${profile.linkedin.replace(/^https?:\/\//, "")}`} target="_blank" rel="noreferrer">
                <span className="hp-contact-link-icon"><HPIcons.Linkedin /></span>
                <div className="hp-contact-link-body">
                  <div className="hp-contact-link-label">LinkedIn</div>
                  <div className="hp-contact-link-val">{profile.linkedin}</div>
                </div>
                <span className="hp-contact-link-arrow">→</span>
              </a>
            )}
            {profile.wechat && (
              <button className="hp-contact-link" onClick={() => copy(profile.wechat, "wechat")}>
                <span className="hp-contact-link-icon"><HPIcons.WeChat /></span>
                <div className="hp-contact-link-body">
                  <div className="hp-contact-link-label">WeChat · 微信</div>
                  <div className="hp-contact-link-val">{profile.wechat}</div>
                </div>
                <span className="hp-contact-link-arrow">
                  {copied === "wechat" ? <span className="hp-copied">{t("✓ Copied", "✓ 已复制")}</span> : <HPIcons.Copy />}
                </span>
              </button>
            )}
            {profile.instagram && (
              <a className="hp-contact-link" href={`https://instagram.com/${profile.instagram.replace("@", "")}`} target="_blank" rel="noreferrer">
                <span className="hp-contact-link-icon"><HPIcons.Instagram /></span>
                <div className="hp-contact-link-body">
                  <div className="hp-contact-link-label">Instagram</div>
                  <div className="hp-contact-link-val">{profile.instagram}</div>
                </div>
                <span className="hp-contact-link-arrow">→</span>
              </a>
            )}
            {profile.email && (
              <a className="hp-contact-link hp-contact-link-email" href={`mailto:${profile.email}`}>
                <span className="hp-contact-link-icon"><HPIcons.Mail /></span>
                <div className="hp-contact-link-body">
                  <div className="hp-contact-link-label">Email</div>
                  <div className="hp-contact-link-val">{profile.email}</div>
                </div>
                <span className="hp-contact-link-arrow">→</span>
              </a>
            )}
          </div>

          <p className="hp-italic hp-contact-foot">
            <span className="hp-sparkle">✦</span> {t("Be kind. They were where you are, not long ago.", "请友善地交流——不久前他们也曾走在你正在走的路上。")}
          </p>

          <div className="hp-contact-guidelines">
            <strong>{t("How to reach out responsibly", "如何得体地联系")}</strong>
            {window.__HP_LANG === "zh" ? (
              <ul>
                <li>仅限一对一、个性化的私信——不要群发模板。</li>
                <li>禁止招聘、销售或产品推广。</li>
                <li>尊重对方不回复——校友很忙,没有义务回复。</li>
                <li>切勿把对方的联系方式转给第三方。</li>
              </ul>
            ) : (
              <ul>
                <li>One-to-one, personal message only — no copy-pasted templates.</li>
                <li>No recruiting, sales, or product pitches.</li>
                <li>Respect a non-reply — alumni are busy and not obligated to respond.</li>
                <li>Never share their contact info with third parties.</li>
              </ul>
            )}
          </div>
        </div>
      </div>
    </div>
  );
};

// ── Feedback modal ──────────────────────────────────────────
const FeedbackModal = ({ isOpen, onClose, backendReady }) => {
  const [message, setMessage] = useState("");
  const [contact, setContact] = useState("");
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState("");
  const [done, setDone] = useState(false);

  useEffect(() => {
    if (isOpen) { setMessage(""); setContact(""); setError(""); setDone(false); setBusy(false); }
  }, [isOpen]);

  if (!isOpen) return null;

  const submit = async () => {
    if (!message.trim()) { setError("Please write a message first. · 请先写点内容。"); return; }
    if (!backendReady || !window.HuskyBackend) {
      setError("Backend not ready — try again later. · 后端未就绪。");
      return;
    }
    setBusy(true); setError("");
    try {
      await window.HuskyBackend.submitFeedback({
        message,
        contact,
        page: (typeof location !== "undefined" ? location.href : ""),
      });
      setDone(true);
    } catch (err) {
      setError(err?.message || "Could not send feedback. · 提交失败,请稍后再试。");
    } finally {
      setBusy(false);
    }
  };

  return (
    <div className="hp-modal-scrim" onClick={onClose}>
      <div className="hp-modal hp-modal-narrow" onClick={(e) => e.stopPropagation()}>
        <div className="hp-modal-head">
          <div>
            <h2 className="hp-modal-title">{t("Feedback", "反馈")}</h2>
            <p className="hp-italic" style={{ marginTop: 6 }}>
              {t("Found a bug, missing a city, or have an idea? Tell us.", "发现问题、想加城市、有想法?告诉我们。")}
            </p>
          </div>
          <button className="hp-close-btn" onClick={onClose}><HPIcons.X /></button>
        </div>

        {done ? (
          <div className="hp-feedback-done">
            <div className="hp-success-sparkle">✦</div>
            <p style={{ fontWeight: 600, margin: "6px 0 2px" }}>{t("Thank you!", "收到啦,谢谢!")}</p>
            <p className="hp-italic" style={{ opacity: 0.85 }}>{t("Your note helps shape Husky Paths.", "你的反馈会帮助 Husky Paths 变得更好。")}</p>
            <div className="hp-success-actions" style={{ marginTop: 16 }}>
              <button className="hp-btn-primary" onClick={onClose}>{t("Close", "关闭")}</button>
            </div>
          </div>
        ) : (
          <div className="hp-form">
            {error && <div className="hp-form-error">{error}</div>}
            <div className="hp-field">
              <label>{t("Your message", "你的留言")} <span className="hp-req">*</span></label>
              <textarea
                value={message}
                onChange={(e) => setMessage(e.target.value)}
                rows={5}
                maxLength={5000}
                placeholder={t("What's working, what's broken, what city we're missing…", "哪里好用、哪里有问题、缺了哪个城市…")}
              />
            </div>
            <div className="hp-field">
              <label>{t("Email or handle", "邮箱或账号")} <span className="hp-field-meta">{t("optional", "可选(方便回复你)")}</span></label>
              <input
                value={contact}
                onChange={(e) => setContact(e.target.value)}
                placeholder="you@example.com"
              />
            </div>
            <div className="hp-form-foot">
              <button type="button" className="hp-btn-ghost" onClick={onClose} disabled={busy}>{t("Cancel", "取消")}</button>
              <button type="button" className="hp-btn-primary" onClick={submit} disabled={busy || !message.trim()}>
                {busy ? t("Sending…", "发送中…") : t("Send feedback", "发送反馈")} <span className="hp-btn-arrow">→</span>
              </button>
            </div>
          </div>
        )}
      </div>
    </div>
  );
};

// ── SubmitForReviewModal — 失效 @uw.edu 的校友走审核队列 ──────
const SubmitForReviewModal = ({ isOpen, onClose, backendReady, initialData = null }) => {
  const blank = { name: "", major: "", year: "2024", pathType: "Industry", orgName: "", roleDetail: "", city: "", note: "", contactHint: "", openToConnect: true, linkedin: "", wechat: "", instagram: "" };
  const [data, setData] = useState(blank);
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState("");
  const [done, setDone] = useState(false);

  useEffect(() => {
    if (isOpen) {
      // 若从 Add path 带着已填内容(非 UW 邮箱)转过来,预填;否则空白
      setData(initialData ? { ...blank, ...initialData } : blank);
      setBusy(false); setError(""); setDone(false);
    }
  }, [isOpen]);

  if (!isOpen) return null;

  const submit = async () => {
    setError("");
    if (!data.orgName.trim() || !data.city) { setError(t("Company/school and city are required.", "请填写机构和城市。")); return; }
    if (!data.contactHint.trim()) { setError(t("Add a personal email or LinkedIn so we can verify you're a UW alum.", "请留个人邮箱或 LinkedIn,方便我们核实你是 UW 校友。")); return; }
    if (!backendReady || !window.HuskyBackend) { setError(t("Backend not ready — try again later.", "后端未就绪,请稍后再试。")); return; }
    setBusy(true);
    try { await window.HuskyBackend.submitForReview(data); setDone(true); }
    catch (err) { setError(err?.message || t("Could not submit. Try again.", "提交失败,请重试。")); }
    finally { setBusy(false); }
  };

  return (
    <div className="hp-modal-scrim" onClick={onClose}>
      <div className="hp-modal" onClick={(e) => e.stopPropagation()}>
        <div className="hp-modal-head">
          <div>
            <Overline en="Alumni · review queue" zh="校友 · 审核通道" color="var(--accent-coral)" />
            <h2 className="hp-modal-title">{t("Share your Husky path", "分享你的 Husky 去向")}</h2>
            <p className="hp-italic" style={{ marginTop: 6 }}>
              {t("No active @uw.edu needed — submit with any email or LinkedIn. We'll verify you're a Husky and add you to the map. (Destination is public; your contact stays private unless you choose to connect.)",
                 "无需在用 @uw.edu——用任意邮箱或 LinkedIn 提交即可。我们核实你是 Husky 后会加到地图上。(去向公开;除非你选择开放联系,联系方式保持私密。)")}
            </p>
          </div>
          <button className="hp-close-btn" onClick={onClose}><HPIcons.X /></button>
        </div>

        {done ? (
          <div className="hp-feedback-done">
            <div className="hp-success-sparkle">✦</div>
            <p style={{ fontWeight: 600, margin: "6px 0 2px" }}>{t("Submitted for review!", "已提交审核!")}</p>
            <p className="hp-italic" style={{ opacity: 0.85 }}>{t("We'll verify and add you to the map soon. Thanks for helping the next class.", "我们会尽快核实并把你加到地图上。谢谢你帮助下一届。")}</p>
            <div className="hp-success-actions" style={{ marginTop: 16 }}>
              <button className="hp-btn-primary" onClick={onClose}>{t("Close", "关闭")}</button>
            </div>
          </div>
        ) : (
          <div className="hp-form">
            {error && <div className="hp-form-error">{error}</div>}

            <div className="hp-form-grid">
              <div className="hp-field">
                <label>{t("Name", "姓名")} <span className="hp-field-meta">{t("optional", "可选")}</span></label>
                <input value={data.name} onChange={(e) => setData({ ...data, name: e.target.value })} placeholder={t("Leave blank for anon", "留空即匿名")}/>
              </div>
              <div className="hp-field">
                <label>{t("Grad year", "毕业年份")}</label>
                <select value={data.year} onChange={(e) => setData({ ...data, year: e.target.value })}>
                  {[2026,2025,2024,2023,2022,2021,2020,2019,2018,2017,2016].map((y) => <option key={y}>{y}</option>)}
                </select>
              </div>
            </div>

            <div className="hp-field">
              <label>{t("Next step", "下一步去向")} <span className="hp-req">*</span></label>
              <div className="hp-seg">
                {PATH_TYPES.map((pt) => (
                  <button key={pt.id} type="button" className={`hp-seg-btn ${data.pathType === pt.id ? "is-active" : ""}`} onClick={() => setData({ ...data, pathType: pt.id })}>
                    <span className="hp-path-glyph">{pt.glyph}</span>
                    <span>{t(pt.en, pt.zh)}</span>
                  </button>
                ))}
              </div>
            </div>

            <div className="hp-form-grid">
              <div className="hp-field">
                <label>{t("Company / School", "公司 / 学校")} <span className="hp-req">*</span></label>
                <input value={data.orgName} onChange={(e) => setData({ ...data, orgName: e.target.value })} placeholder={t("Where to?", "去了哪里?")} required/>
              </div>
              <div className="hp-field">
                <label>{t("City", "城市")} <span className="hp-req">*</span></label>
                <select value={data.city} onChange={(e) => setData({ ...data, city: e.target.value })} required>
                  <option value="" disabled>{t("Select a city", "选择城市")}</option>
                  {REGIONS.map((r) => (
                    <optgroup key={r} label={r}>
                      {CITIES.filter((c) => c.region === r).sort((a, b) => a.name.localeCompare(b.name)).map((c) => (
                        <option key={c.name} value={c.name}>{c.name}{c.zh ? ` · ${c.zh}` : ""}</option>
                      ))}
                    </optgroup>
                  ))}
                </select>
              </div>
            </div>

            <div className="hp-field">
              <label>{t("Major", "专业")} <span className="hp-field-meta">{t("optional", "可选")}</span></label>
              <input value={data.major} onChange={(e) => setData({ ...data, major: e.target.value })} placeholder={t("e.g. Computer Science", "如:计算机科学")}/>
            </div>

            <div className="hp-field">
              <label>{t("Verify you're a UW alum", "证明你是 UW 校友")} <span className="hp-req">*</span></label>
              <input value={data.contactHint} onChange={(e) => setData({ ...data, contactHint: e.target.value })} placeholder={t("Personal email or LinkedIn URL", "个人邮箱 或 LinkedIn 链接")}/>
              <span className="hp-field-meta" style={{ marginTop: 4, display: "block" }}>
                {t("Private — used only to verify you, never shown on the map.", "仅用于核实身份,不会显示在地图上。")}
              </span>
            </div>

            <label className="hp-toggle">
              <input type="checkbox" checked={data.openToConnect} onChange={(e) => setData({ ...data, openToConnect: e.target.checked })} />
              <span className="hp-toggle-track"><span className="hp-toggle-thumb" /></span>
              <span className="hp-toggle-body">
                <strong>{t("Open to connect", "开放联系")}</strong>
                <span>{t("Let fellow Huskies reach out. Add a handle below — shown only after we verify & publish you.", "让校友能联系到你。下面填个账号——核实通过、上墙后才会显示。")}</span>
              </span>
            </label>

            {data.openToConnect && (
              <div className="hp-social-block">
                <Overline en="Contact (optional)" zh="联系方式(选填)" color="var(--accent-coral)" />
                <div className="hp-social-row">
                  <HPIcons.Linkedin />
                  <input value={data.linkedin} onChange={(e) => setData({ ...data, linkedin: e.target.value })} placeholder="linkedin.com/in/your-handle"/>
                </div>
                <div className="hp-social-row">
                  <HPIcons.WeChat />
                  <input value={data.wechat} onChange={(e) => setData({ ...data, wechat: e.target.value })} placeholder={t("WeChat ID · 微信号", "微信号")}/>
                </div>
                <div className="hp-social-row">
                  <HPIcons.Instagram />
                  <input value={data.instagram} onChange={(e) => setData({ ...data, instagram: e.target.value })} placeholder="@your.handle"/>
                </div>
              </div>
            )}

            <div className="hp-form-foot">
              <button type="button" className="hp-btn-ghost" onClick={onClose} disabled={busy}>{t("Cancel", "取消")}</button>
              <button type="button" className="hp-btn-primary" onClick={submit} disabled={busy || !data.orgName.trim() || !data.city}>
                {busy ? t("Submitting…", "提交中…") : t("Submit for review", "提交审核")} <span className="hp-btn-arrow">→</span>
              </button>
            </div>
          </div>
        )}
      </div>
    </div>
  );
};

window.SubmitFlow = SubmitFlow;
window.VerifyAccessModal = VerifyAccessModal;
window.ContactModal = ContactModal;
window.FeedbackModal = FeedbackModal;
window.SubmitForReviewModal = SubmitForReviewModal;
window.PathTypeGlyph = PathTypeGlyph;
