/* ===== Luxury Academy — Modals ===== */

// ── Toast ─────────────────────────────────────────────────────────────────────
function Toast({ toast, onClose }) {
  React.useEffect(() => {
    if (!toast) return;
    const id = setTimeout(onClose, 3500);
    return () => clearTimeout(id);
  }, [toast]);
  if (!toast) return null;
  return (
    <div style={{
      position:"fixed", bottom:24, left:"50%", transform:"translateX(-50%)",
      background: toast.type === "ok" ? "#2E7D5B" : "#A8286A",
      color:"white", padding:"12px 24px", borderRadius:12, zIndex:9999,
      fontSize:14, fontWeight:600, boxShadow:"0 8px 32px rgba(0,0,0,0.35)",
      display:"flex", alignItems:"center", gap:10, minWidth:260
    }}>
      <span>{toast.type === "ok" ? "✓" : "✕"}</span>
      <span>{toast.msg}</span>
      <button onClick={onClose} style={{marginLeft:"auto", background:"none", border:"none", color:"white", cursor:"pointer", fontSize:18, lineHeight:1}}>×</button>
    </div>
  );
}

// ── Enroll Modal ──────────────────────────────────────────────────────────────
function EnrollModal({ course, lang, onClose }) {
  const [name, setName] = React.useState("");
  const [phone, setPhone] = React.useState("");
  const [sent, setSent] = React.useState(false);

  if (!course) return null;

  const handleSubmit = (e) => {
    e.preventDefault();
    const msg = lang === "es"
      ? `Hola! Me interesa inscribirme al curso *${course.title.es}* (${course.price.es}). Mi nombre es ${name} y mi número es ${phone}.`
      : `Hello! I'm interested in enrolling in *${course.title.en}* (${course.price.en}). My name is ${name}, phone: ${phone}.`;
    window.open(`https://wa.me/573332716890?text=${encodeURIComponent(msg)}`, "_blank");
    setSent(true);
  };

  return (
    <div style={{
      position:"fixed", inset:0, background:"rgba(39,21,40,0.85)", backdropFilter:"blur(6px)",
      zIndex:9000, display:"flex", alignItems:"center", justifyContent:"center", padding:20
    }} onClick={(e) => e.target === e.currentTarget && onClose()}>
      <div style={{
        background:"var(--c-navy-800, #4A2A46)", border:"1px solid rgba(216,126,158,0.25)",
        borderRadius:"var(--radius-xl,20px)", padding:"36px 32px", maxWidth:480, width:"100%",
        position:"relative", boxShadow:"0 32px 80px rgba(0,0,0,0.6)"
      }}>
        <button onClick={onClose} style={{
          position:"absolute", top:16, right:16, background:"none", border:"none",
          color:"rgba(251,248,242,0.5)", cursor:"pointer", fontSize:22, lineHeight:1
        }}>×</button>

        <div style={{fontSize:11, letterSpacing:"0.2em", color:"var(--c-gold-400)", textTransform:"uppercase", marginBottom:8}}>
          {lang === "es" ? "Inscripción" : "Enrollment"}
        </div>
        <h3 style={{margin:"0 0 4px", color:"var(--c-cream, #FBF8F2)", fontSize:22}}>
          {course.title[lang]}
        </h3>
        <div style={{color:"var(--c-gold-400)", fontSize:15, fontWeight:700, marginBottom:24}}>
          {course.price[lang]}
        </div>

        {sent ? (
          <div style={{textAlign:"center", padding:"24px 0"}}>
            <div style={{fontSize:48, marginBottom:12}}>✓</div>
            <p style={{color:"rgba(251,248,242,0.8)", fontSize:15}}>
              {lang === "es"
                ? "¡Te redirigimos a WhatsApp! Nuestro equipo te contactará pronto."
                : "Redirecting to WhatsApp! Our team will contact you shortly."}
            </p>
            <button onClick={onClose} className="btn btn-gold" style={{marginTop:16}}>
              {lang === "es" ? "Cerrar" : "Close"}
            </button>
          </div>
        ) : (
          <form onSubmit={handleSubmit} style={{display:"flex", flexDirection:"column", gap:14}}>
            <div>
              <label style={{display:"block", fontSize:12, color:"rgba(251,248,242,0.6)", marginBottom:6, letterSpacing:"0.05em"}}>
                {lang === "es" ? "Nombre completo" : "Full name"}
              </label>
              <input
                required value={name} onChange={e => setName(e.target.value)}
                style={{
                  width:"100%", boxSizing:"border-box",
                  background:"rgba(216,126,158,0.06)", border:"1px solid rgba(216,126,158,0.2)",
                  borderRadius:10, padding:"12px 14px", color:"var(--c-cream,#FBF8F2)",
                  fontSize:14, outline:"none"
                }}
                placeholder={lang === "es" ? "Tu nombre" : "Your name"}
              />
            </div>
            <div>
              <label style={{display:"block", fontSize:12, color:"rgba(251,248,242,0.6)", marginBottom:6, letterSpacing:"0.05em"}}>
                {lang === "es" ? "WhatsApp / Teléfono" : "WhatsApp / Phone"}
              </label>
              <input
                required value={phone} onChange={e => setPhone(e.target.value)}
                style={{
                  width:"100%", boxSizing:"border-box",
                  background:"rgba(216,126,158,0.06)", border:"1px solid rgba(216,126,158,0.2)",
                  borderRadius:10, padding:"12px 14px", color:"var(--c-cream,#FBF8F2)",
                  fontSize:14, outline:"none"
                }}
                placeholder="+57 300 000 0000"
              />
            </div>
            <button type="submit" className="btn btn-gold" style={{marginTop:8, width:"100%", padding:"14px"}}>
              {lang === "es" ? "Solicitar información por WhatsApp" : "Request info via WhatsApp"}
            </button>
            <p style={{fontSize:11, color:"rgba(251,248,242,0.4)", textAlign:"center", margin:0}}>
              {lang === "es"
                ? "Al continuar serás redirigido a WhatsApp."
                : "Continuing will redirect you to WhatsApp."}
            </p>
          </form>
        )}
      </div>
    </div>
  );
}

window.Toast = Toast;
window.EnrollModal = EnrollModal;
