/* ===== Luxury Academy — App Root ===== */

function App() {
  const [lang, setLang] = React.useState("es");
  const [toast, setToast] = React.useState(null);

  // Live data from Supabase (fallback: static constants from data.jsx)
  const [services,     setServices]     = React.useState(SERVICES);
  const [courses,      setCourses]      = React.useState(COURSES);
  const [testimonials, setTestimonials] = React.useState(TESTIMONIALS);
  const [dataReady,    setDataReady]    = React.useState(false);

  React.useEffect(() => {
    Promise.all([
      lxApi.getServices(),
      lxApi.getCourses(),
      lxApi.getTestimonials(),
    ]).then(([svcs, cs, testis]) => {
      if (svcs.length)   setServices(svcs);
      if (cs.length)     setCourses(cs);
      if (testis.length) setTestimonials(testis);
    }).catch(err => {
      console.warn("Supabase fetch failed, using static fallback:", err);
    }).finally(() => setDataReady(true));
  }, []);

  const t = TRANSLATIONS[lang];

  // Expose a global toast so non-App components (cart, etc.) can trigger it
  React.useEffect(() => {
    window.lxToast = (payload) => setToast(payload);
    return () => { delete window.lxToast; };
  }, []);

  // Init scroll effects once the tree is mounted (run again when async data lands)
  React.useEffect(() => {
    if (window.lxInitScrollFX) window.lxInitScrollFX();
  }, []);
  React.useEffect(() => {
    if (window.lxObserveFX) window.lxObserveFX();
  }, [services, courses, testimonials, dataReady]);

  const scrollTo = (id) => {
    const el = document.getElementById(id);
    if (el) el.scrollIntoView({ behavior: "smooth", block: "start" });
  };

  return (
    <>
      <Nav lang={lang} setLang={setLang} t={t} scrollTo={scrollTo} />
      <Hero lang={lang} t={t} scrollTo={scrollTo} />
      <Services lang={lang} t={t} services={services} />
      <Courses lang={lang} t={t} courses={courses} />
      <Offers lang={lang} />
      <Gallery lang={lang} t={t} />
      <VideoSection lang={lang} />
      <Testimonials lang={lang} t={t} testimonials={testimonials} />
      <About lang={lang} t={t} />
      <FAQ lang={lang} t={t} />
      <Contact lang={lang} t={t} />
      <Newsletter lang={lang} t={t} />
      <Footer lang={lang} t={t} scrollTo={scrollTo} services={services} courses={courses} />
      <WhatsAppFAB lang={lang} />
      <BackToTop />
      <StickyCTA lang={lang} scrollTo={scrollTo} />
      <CartDrawer lang={lang} scrollTo={scrollTo} />
      <CartCheckout lang={lang} t={t} />
      <Toast toast={toast} onClose={() => setToast(null)} />
    </>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
