/* ===== Luxury Academy — Sections 1: Nav, Hero, Services, Courses ===== */

// ── Luxury logo mark SVG — rose-gold lash emblem ─────────────────────────────
function LuxLogoMark({ size = 32 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 40 40" fill="none" aria-hidden="true">
      <defs>
        <linearGradient id="lxLogoGrad" x1="6" y1="4" x2="34" y2="36" gradientUnits="userSpaceOnUse">
          <stop offset="0" stopColor="#F7D8E3"/>
          <stop offset="0.5" stopColor="#E08FAB"/>
          <stop offset="1" stopColor="#C2607F"/>
        </linearGradient>
      </defs>
      <rect width="40" height="40" rx="11" fill="url(#lxLogoGrad)"/>
      {/* soft top sheen */}
      <rect width="40" height="19" rx="11" fill="#FFFFFF" opacity="0.10"/>
      {/* lash eye */}
      <path d="M9 25 Q20 16 31 25" stroke="#FFFFFF" strokeWidth="1.8" fill="none" strokeLinecap="round"/>
      <path d="M9 25 Q20 30 31 25" stroke="#FFFFFF" strokeWidth="1.3" fill="none" strokeLinecap="round" opacity="0.65"/>
      {/* lashes */}
      <path d="M11 22.6 L9 19"      stroke="#FFFFFF" strokeWidth="1.4" strokeLinecap="round"/>
      <path d="M15.4 20 L14.4 15.8" stroke="#FFFFFF" strokeWidth="1.4" strokeLinecap="round"/>
      <path d="M20 19.2 L20 14.8"   stroke="#FFFFFF" strokeWidth="1.5" strokeLinecap="round"/>
      <path d="M24.6 20 L25.6 15.8" stroke="#FFFFFF" strokeWidth="1.4" strokeLinecap="round"/>
      <path d="M29 22.6 L31 19"     stroke="#FFFFFF" strokeWidth="1.4" strokeLinecap="round"/>
      {/* iris */}
      <circle cx="20" cy="24" r="2.3" fill="#FFFFFF"/>
      {/* sparkle */}
      <path d="M30 7.5 l0.95 2.15 2.15 0.95 -2.15 0.95 -0.95 2.15 -0.95 -2.15 -2.15 -0.95 2.15 -0.95 z" fill="#FFFFFF"/>
    </svg>
  );
}

// ── Nav ───────────────────────────────────────────────────────────────────────
function Nav({ lang, setLang, t, scrollTo }) {
  const [menuOpen, setMenuOpen] = React.useState(false);

  return (
    <header className="nav">
      <div className="wrap nav-row">
        <a className="nav-brand" href="#top" onClick={e => { e.preventDefault(); scrollTo("top"); }}>
          <div className="nav-logo-mark"><LuxLogoMark size={28}/></div>
          <div>
            <div className="nav-brand-text">{t.footer.brand}</div>
            <div className="nav-brand-sub">{t.footer.tagline}</div>
          </div>
        </a>

        <nav className="nav-links">
          {[
            { key: "services", label: t.nav.services },
            { key: "courses",  label: t.nav.courses  },
            { key: "offers",   label: t.nav.offers   },
            { key: "about",    label: t.nav.about    },
            { key: "gallery",  label: t.nav.gallery  },
            { key: "contact",  label: t.nav.contact  },
          ].map(item => (
            <a key={item.key} className="nav-link" href={`#${item.key}`}
               onClick={e => { e.preventDefault(); scrollTo(item.key); }}>
              {item.label}
            </a>
          ))}
        </nav>

        <div className="nav-cta">
          <div className="lang-toggle">
            <button className={lang === "es" ? "on" : ""} onClick={() => setLang("es")}>ES</button>
            <button className={lang === "en" ? "on" : ""} onClick={() => setLang("en")}>EN</button>
          </div>
          <button className="btn btn-ghost btn-small" onClick={() => scrollTo("courses")}>{t.nav.enroll}</button>
          <button className="btn btn-gold btn-small"  onClick={() => cartStore.openDrawer()}>{t.nav.bookNow}</button>
          <CartButton lang={lang}/>
          <button className="menu-toggle" onClick={() => setMenuOpen(true)} aria-label="Menu">
            <Icon.Menu/>
          </button>
        </div>
      </div>

      {menuOpen && (
        <div className="modal-back" onClick={() => setMenuOpen(false)}>
          <div className="modal" onClick={e => e.stopPropagation()} style={{padding:30}}>
            <button className="modal-close" onClick={() => setMenuOpen(false)}><Icon.Close/></button>
            <div style={{display:"flex", flexDirection:"column", gap:6, marginTop:30}}>
              {["services","courses","offers","about","gallery","contact"].map(s => (
                <a key={s} className="nav-link" style={{padding:"14px 16px", fontSize:18}}
                   href={`#${s}`} onClick={() => { setMenuOpen(false); setTimeout(() => scrollTo(s), 50); }}>
                  {t.nav[s]}
                </a>
              ))}
            </div>
          </div>
        </div>
      )}
    </header>
  );
}

// ── Hero ──────────────────────────────────────────────────────────────────────
function Hero({ lang, t, scrollTo }) {
  return (
    <section id="top" className="hero">
      <div className="wrap hero-grid">
        {/* Left: text */}
        <div className="fade-in">
          <div className="eyebrow hero-eyebrow"><span className="dot"></span>{t.hero.eyebrow}</div>
          <h1 className="hero-title">
            {t.hero.title1}<br/>
            <span className="gold">{t.hero.title2}</span>
          </h1>
          <p className="hero-sub">{t.hero.sub}</p>
          <div className="hero-ctas">
            <button className="btn btn-gold" onClick={() => scrollTo("services")}>
              <Icon.Sparkle size={16}/> {t.hero.ctaServices}
            </button>
            <button className="btn btn-outline-gold" onClick={() => scrollTo("courses")}>
              {t.hero.ctaCourses}
            </button>
          </div>
          <div className="hero-stats">
            <div className="stat"><span className="num">{t.hero.stat1}</span><span className="lbl">{t.hero.stat1l}</span></div>
            <div className="stat"><span className="num">{t.hero.stat2}</span><span className="lbl">{t.hero.stat2l}</span></div>
            <div className="stat"><span className="num">{t.hero.stat3}</span><span className="lbl">{t.hero.stat3l}</span></div>
          </div>
        </div>

        {/* Right: hero photo */}
        <div className="hero-visual fade-in">
          <div className="hero-img-wrap">
            <img src="assets/hero-students.png" alt="Profesionales Luxury Academy Ibagué"/>
            {/* gradient blend hacia la izquierda */}
            <div className="hero-img-fade"/>
          </div>

          {/* Badge flotante */}
          <div className="hero-badge-gold">
            <div style={{fontSize:10, letterSpacing:"0.18em", marginBottom:4}}>LUXURY</div>
            <div style={{fontFamily:"var(--font-display)", fontSize:14, lineHeight:1.2}}>Academy<br/>Ibagué</div>
          </div>

          {/* Pill flotante */}
          <div className="hero-pill">
            <div style={{fontSize:9, letterSpacing:"0.22em", opacity:0.5, marginBottom:3}}>CERTIFICACIÓN PROFESIONAL</div>
            <div style={{fontFamily:"var(--font-display)", fontSize:15, color:"var(--c-gold-500)"}}>+15 programas</div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── Service image (gradient SVG) ───────────────────────────────────────────────
function ServiceImage({ svc }) {
  const [failed, setFailed] = React.useState(false);
  if (svc.img && !failed) {
    return (
      <img
        src={svc.img}
        alt={svc.name ? (typeof svc.name === "object" ? svc.name.es : svc.name) : ""}
        style={{width:"100%",height:"100%",objectFit:"cover",display:"block"}}
        loading="lazy"
        onError={() => setFailed(true)}
      />
    );
  }
  return (
    <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" style={{width:"100%",height:"100%"}}>
      <defs>
        <linearGradient id={`lg-${svc.id}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stopColor={svc.swatchFrom}/>
          <stop offset="1" stopColor={svc.swatchTo}/>
        </linearGradient>
        <radialGradient id={`rg-${svc.id}`} cx="0.7" cy="0.3" r="0.65">
          <stop offset="0" stopColor="white" stopOpacity="0.22"/>
          <stop offset="1" stopColor="white" stopOpacity="0"/>
        </radialGradient>
      </defs>
      <rect width="400" height="300" fill={`url(#lg-${svc.id})`}/>
      <rect width="400" height="300" fill={`url(#rg-${svc.id})`}/>
      {/* category-specific illustration */}
      {svc.cat === "lashes" && (
        <g>
          {[120,145,170,195,220,245,270,295].map((x,i) => (
            <path key={i} d={`M ${x} 180 Q ${x+(i%2===0?-10:10)} ${130+(i%3)*15} ${x+(i%2===0?-18:18)} ${100+(i%3)*20}`}
              stroke="white" strokeWidth={i%3===0?2:1.5} fill="none" strokeLinecap="round" opacity="0.85"/>
          ))}
          <ellipse cx="210" cy="182" rx="110" ry="10" fill="black" opacity="0.4"/>
          <text x="205" y="240" textAnchor="middle" fill="white" opacity="0.35" fontFamily="serif" fontStyle="italic" fontSize="22">lashes</text>
        </g>
      )}
      {svc.cat === "brows" && (
        <g>
          {[1,-1].map((d,i) => (
            <path key={i} d={`M ${100+i*120} 165 Q ${160+i*80} ${130} ${220+i*40} 160`}
              stroke="white" strokeWidth="8" fill="none" strokeLinecap="round" opacity="0.75"/>
          ))}
          <text x="200" y="240" textAnchor="middle" fill="white" opacity="0.3" fontFamily="serif" fontStyle="italic" fontSize="20">diseño</text>
        </g>
      )}
      {svc.cat === "micropig" && (
        <g>
          <circle cx="200" cy="150" r="60" fill="white" opacity="0.1"/>
          {[0,60,120,180,240,300].map((angle,i) => (
            <circle key={i} cx={200+55*Math.cos(angle*Math.PI/180)} cy={150+55*Math.sin(angle*Math.PI/180)} r="5" fill="white" opacity="0.6"/>
          ))}
          <circle cx="200" cy="150" r="20" fill="white" opacity="0.2"/>
          <text x="200" y="240" textAnchor="middle" fill="white" opacity="0.3" fontFamily="serif" fontStyle="italic" fontSize="18">micropigmentación</text>
        </g>
      )}
      {svc.cat === "nails" && (
        <g>
          {[100,130,160,190,220,250,280,310].map((x,i) => (
            <rect key={i} x={x} y={140-(i%3)*15} width="22" height="35" rx="11" fill="white" opacity="0.6+(i%3)*0.1"/>
          ))}
          <text x="205" y="240" textAnchor="middle" fill="white" opacity="0.3" fontFamily="serif" fontStyle="italic" fontSize="22">nails</text>
        </g>
      )}
      {svc.cat === "facial" && (
        <g>
          <circle cx="200" cy="145" r="65" fill="white" opacity="0.15"/>
          <circle cx="200" cy="145" r="42" fill="white" opacity="0.15"/>
          <circle cx="180" cy="130" r="8" fill="white" opacity="0.6"/>
          <circle cx="220" cy="130" r="8" fill="white" opacity="0.6"/>
          <path d="M 180 165 Q 200 180 220 165" stroke="white" strokeWidth="2.5" fill="none" strokeLinecap="round" opacity="0.6"/>
          <text x="200" y="240" textAnchor="middle" fill="white" opacity="0.3" fontFamily="serif" fontStyle="italic" fontSize="20">facial</text>
        </g>
      )}
      {svc.cat === "waxing" && (
        <g>
          <ellipse cx="200" cy="145" rx="80" ry="25" fill="white" opacity="0.12"/>
          <rect x="155" y="120" width="90" height="50" rx="8" fill="white" opacity="0.1"/>
          <path d="M 160 145 Q 200 110 240 145" stroke="white" strokeWidth="2" fill="none" opacity="0.5" strokeLinecap="round"/>
          <text x="200" y="240" textAnchor="middle" fill="white" opacity="0.3" fontFamily="serif" fontStyle="italic" fontSize="20">depilación</text>
        </g>
      )}
      {/* sparkles */}
      <circle cx="340" cy="50" r="2.5" fill="white" opacity="0.7"/>
      <circle cx="60" cy="220" r="2" fill="white" opacity="0.6"/>
      <circle cx="360" cy="200" r="2" fill="white" opacity="0.55"/>
    </svg>
  );
}

// ── Services ──────────────────────────────────────────────────────────────────
function Services({ lang, t, onBook, services: svcsProp }) {
  const services = svcsProp ?? SERVICES;
  const [cat, setCat] = React.useState("all");
  const [query, setQuery] = React.useState("");
  const [sort, setSort] = React.useState("default");
  const cats = ["all","lashes","brows","micropig","nails","facial","waxing"];
  const fmt = n => "$" + n.toLocaleString(lang === "en" ? "en-US" : "es-CO");

  let filtered = cat === "all" ? services : services.filter(s => s.cat === cat);
  const q = query.trim().toLowerCase();
  if (q) filtered = filtered.filter(s =>
    s.name[lang].toLowerCase().includes(q) ||
    (s.desc[lang] && s.desc[lang].toLowerCase().includes(q)) ||
    (s.includes[lang] && s.includes[lang].some(inc => inc.toLowerCase().includes(q)))
  );
  if (sort === "price-asc")  filtered = [...filtered].sort((a, b) => a.price - b.price);
  if (sort === "price-desc") filtered = [...filtered].sort((a, b) => b.price - a.price);
  const total = (cat === "all" ? services : services.filter(s => s.cat === cat)).length;

  return (
    <section id="services" className="section">
      <div className="wrap">
        <div className="section-head">
          <div className="eyebrow"><span className="dot"></span>{t.services.eyebrow}</div>
          <h2>{t.services.title1} <span className="alt">{t.services.title2}</span></h2>
          <p>{t.services.sub}</p>
        </div>
        <div className="svc-filter">
          {cats.map(c => (
            <button key={c} className={`svc-chip ${cat === c ? "on" : ""}`} onClick={() => setCat(c)}>
              {t.services.categories[c]}
            </button>
          ))}
        </div>
        <div className="svc-toolbar">
          <div className="svc-search">
            <Icon.Search size={16}/>
            <input
              type="search"
              value={query}
              onChange={e => setQuery(e.target.value)}
              placeholder={lang === "es" ? "Buscar servicio…" : "Search service…"}
              aria-label={lang === "es" ? "Buscar servicio" : "Search service"}
            />
            {query && <button className="svc-search-clear" onClick={() => setQuery("")} aria-label={lang === "es" ? "Limpiar" : "Clear"}><Icon.Close size={14}/></button>}
          </div>
          <div className="svc-sort">
            <label htmlFor="svc-sort">{lang === "es" ? "Ordenar:" : "Sort:"}</label>
            <select id="svc-sort" value={sort} onChange={e => setSort(e.target.value)}>
              <option value="default">{lang === "es" ? "Recomendado" : "Recommended"}</option>
              <option value="price-asc">{lang === "es" ? "Precio: menor a mayor" : "Price: low to high"}</option>
              <option value="price-desc">{lang === "es" ? "Precio: mayor a menor" : "Price: high to low"}</option>
            </select>
          </div>
        </div>
        <div className="svc-count">
          {filtered.length === total && !q
            ? (lang === "es" ? `${total} servicios` : `${total} services`)
            : (lang === "es" ? `${filtered.length} de ${total} servicios` : `${filtered.length} of ${total} services`)}
        </div>
        {filtered.length === 0 ? (
          <div className="svc-empty">
            <Icon.Search size={32}/>
            <p>{lang === "es" ? "No encontramos servicios con esos criterios." : "No services match your search."}</p>
            <button className="btn btn-ghost btn-small" onClick={() => { setQuery(""); setCat("all"); }}>
              {lang === "es" ? "Limpiar filtros" : "Clear filters"}
            </button>
          </div>
        ) : (
        <div className="svc-grid">
          {filtered.map(s => (
            <article key={s.id} className="svc-card">
              <div className="simg">
                <ServiceImage svc={s}/>
                {s.badge && <span className="badge">{s.badge[lang]}</span>}
                <span className="tag-dur">{s.duration[lang]}</span>
              </div>
              <div className="sbody">
                <h3>{s.name[lang]}</h3>
                <p className="sdesc">{s.desc[lang]}</p>
                <div className="sincludes">
                  {s.includes[lang].slice(0,4).map((inc,i) => <span key={i}>{inc}</span>)}
                </div>
                <div className="sfoot">
                  <div className="sprice">{fmt(s.price)} <small>COP</small></div>
                  <button className="btn btn-primary btn-small" onClick={() => lxAddToCart(s, lang)}>
                    <Icon.Bag size={14}/> {t.services.bookThis}
                  </button>
                </div>
              </div>
            </article>
          ))}
        </div>
        )}
      </div>
    </section>
  );
}

// ── Courses ───────────────────────────────────────────────────────────────────
function Courses({ lang, t, courses: csProp }) {
  const courses = csProp ?? COURSES;
  const fmt = n => "$" + n.toLocaleString(lang === "en" ? "en-US" : "es-CO");
  const waMsg = (c) => lang === "es"
    ? `Hola Luxury Academy! Me interesa inscribirme al programa: ${c.name.es}. ¿Cuáles son los próximos cupos y fechas?`
    : `Hello Luxury Academy! I'm interested in enrolling in: ${c.name.en}. What are the next available spots and dates?`;

  return (
    <section id="courses" className="section section--dark">
      <div className="wrap">
        <div className="section-head">
          <div className="eyebrow"><span className="dot"></span>{t.courses.eyebrow}</div>
          <h2>{t.courses.title1} <span className="alt">{t.courses.title2}</span></h2>
          <p>{t.courses.sub}</p>
        </div>
        <div className="courses-grid">
          {courses.map((c, i) => (
            <div key={c.id} className="course-card">
              {c.img && (
                <div style={{height:180, overflow:"hidden", borderRadius:"var(--radius-lg) var(--radius-lg) 0 0", margin:"-32px -28px 20px -28px", position:"relative"}}>
                  <img src={c.img} alt={c.name[lang]} style={{width:"100%",height:"100%",objectFit:"cover"}} loading="lazy" onError={e => { e.target.style.display = "none"; }}/>
                  <div style={{position:"absolute",inset:0,background:"linear-gradient(to bottom, transparent 50%, rgba(39,21,40,0.85) 100%)"}}/>
                  <div style={{position:"absolute",bottom:12,left:16,fontSize:28}}>{c.icon}</div>
                  <div style={{position:"absolute",top:12,right:12,background:"rgba(216,126,158,0.2)",border:"1px solid rgba(216,126,158,0.4)",borderRadius:6,padding:"2px 8px",fontSize:11,color:"var(--c-gold-300)",fontWeight:600}}>
                    {String(i+1).padStart(2,"0")}
                  </div>
                </div>
              )}
              {!c.img && <><div className="cnum">{String(i+1).padStart(2,"0")}</div><div className="cicon">{c.icon}</div></>}
              <h3>{c.name[lang]}</h3>
              <p>{c.desc[lang]}</p>
              <div className="cmod">
                {c.modules[lang].map((m, j) => <span key={j}>{m}</span>)}
              </div>
              <div style={{display:"flex", justifyContent:"space-between", alignItems:"flex-end", marginTop:"auto"}}>
                <div>
                  <div className="cdur">⏱ {c.duration[lang]}</div>
                  <div className="cdur" style={{marginTop:4}}>📊 {c.level[lang]}</div>
                  <div style={{fontFamily:"var(--font-display)", fontSize:22, color:"var(--c-gold-300)", marginTop:10}}>
                    {fmt(c.price)}
                  </div>
                </div>
                <a href={`https://wa.me/573332716890?text=${encodeURIComponent(waMsg(c))}`}
                   target="_blank" rel="noopener"
                   className="btn btn-gold btn-small" style={{alignSelf:"flex-end"}}>
                  {t.courses.enroll}
                </a>
              </div>
            </div>
          ))}
        </div>
        <div style={{textAlign:"center", marginTop:48}}>
          <p style={{color:"rgba(251,248,242,0.55)", marginBottom:20}}>
            {lang === "es" ? "¿Buscas un programa personalizado o tienes una empresa?" : "Looking for a custom program or have a business?"}
          </p>
          <a href={`https://wa.me/573332716890?text=${encodeURIComponent(lang === "es" ? "Hola! Me gustaría consultar sobre capacitaciones empresariales o programas personalizados en Luxury Academy." : "Hello! I'd like to ask about corporate training or custom programs at Luxury Academy.")}`}
             target="_blank" rel="noopener" className="btn btn-outline-gold">
            <Icon.Whatsapp size={16}/>
            {lang === "es" ? "Consultar programa a medida" : "Inquire about custom program"}
          </a>
        </div>
      </div>
    </section>
  );
}

window.Nav = Nav;
window.Hero = Hero;
window.Services = Services;
window.Courses = Courses;
window.LuxLogoMark = LuxLogoMark;
