/* app.jsx — root: state, lightbox, tweaks, section orchestration */
const { useState: useStateA, useEffect: useEffectA, useCallback: useCallbackA } = React;

/* ---- Lightbox (shared by gallery + room overlay) ---- */
function Lightbox({ data, onClose, onIndex }) {
  const open = !!data;
  useEffectA(() => {
    if (!open) return;
    const onKey = (e) => {
      if (e.key === "Escape") onClose();
      if (e.key === "ArrowRight") onIndex(1);
      if (e.key === "ArrowLeft") onIndex(-1);
    };
    document.addEventListener("keydown", onKey);
    return () => document.removeEventListener("keydown", onKey);
  }, [open, onClose, onIndex]);

  if (!open) return null;
  const cur = data.imgs[data.i];
  return (
    <div className={"lightbox show"} onClick={onClose} data-screen-label="Lightbox">
      <button className="lb-x" aria-label="Close" onClick={onClose}>&#10005;</button>
      {data.imgs.length > 1 ? (
        <React.Fragment>
          <button className="lb-nav lb-prev" aria-label="Previous" onClick={(e) => { e.stopPropagation(); onIndex(-1); }}>&#8249;</button>
          <button className="lb-nav lb-next" aria-label="Next" onClick={(e) => { e.stopPropagation(); onIndex(1); }}>&#8250;</button>
        </React.Fragment>
      ) : null}
      <img src={cur.src} alt={cur.cap || ""} onClick={(e) => e.stopPropagation()} />
      {cur.cap ? <div className="lb-cap">{cur.cap}</div> : null}
    </div>
  );
}

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "accent": "Blue",
  "hero": "Centered",
  "motion": true,
  "band": "Medium",
  "serif": "Garamond"
}/*EDITMODE-END*/;

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [scrolled, setScrolled] = useStateA(false);
  const [roomIdx, setRoomIdx] = useStateA(null);
  const [lightbox, setLightbox] = useStateA(null);   // { imgs, i }
  const [prefill, setPrefill] = useStateA(null);      // { name, n }
  const [adminOpen, setAdminOpen] = useStateA(false);
  const [bookings, setBookings] = useStateA(() => window.RIAD.reservations.slice());

  /* apply visual tweaks to <body> */
  useEffectA(() => {
    const b = document.body;
    b.setAttribute("data-accent", String(t.accent).toLowerCase());
    b.setAttribute("data-band", String(t.band).toLowerCase());
    b.setAttribute("data-serif", String(t.serif).toLowerCase());
  }, [t.accent, t.band, t.serif]);

  /* header frosted on scroll */
  useEffectA(() => {
    const onScroll = () => setScrolled(window.scrollY > 60);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const openLightbox = useCallbackA((imgs, i) => setLightbox({ imgs, i }), []);
  const navLightbox = useCallbackA((d) => {
    setLightbox((lb) => lb ? { ...lb, i: (lb.i + d + lb.imgs.length) % lb.imgs.length } : lb);
  }, []);

  const reserveRoom = (room) => {
    setRoomIdx(null);
    setPrefill({ name: room.name, n: Date.now() });
    setTimeout(() => window.scrollToId("reservation"), 60);
  };

  const addBooking = (b) => setBookings((prev) => [b, ...prev]);
  const setStatus = (ref, status) => setBookings((prev) => prev.map((b) => b.ref === ref ? { ...b, status } : b));

  const rooms = window.RIAD.rooms;

  return (
    <React.Fragment>
      <Header scrolled={scrolled} />
      <Hero layout={String(t.hero).toLowerCase()} motion={t.motion} />

      <main>
        <Rooms onOpen={setRoomIdx} />
        <Meals />
        <Gallery openLightbox={openLightbox} />
        <Reviews />
        <Story />
        <Reservation prefillRoom={prefill ? prefill.name : null} onSubmitted={addBooking} />
      </main>

      <Footer onStaff={() => setAdminOpen(true)} />
      <Floats />

      <RoomOverlay
        room={roomIdx != null ? rooms[roomIdx] : null}
        onClose={() => setRoomIdx(null)}
        onReserve={reserveRoom}
        openLightbox={openLightbox}
      />

      <Lightbox data={lightbox} onClose={() => setLightbox(null)} onIndex={navLightbox} />

      {adminOpen ? (
        <Dashboard bookings={bookings} onSetStatus={setStatus} onClose={() => setAdminOpen(false)} />
      ) : null}

      <TweaksPanel title="Tweaks">
        <TweakSection label="Accent & surfaces" />
        <TweakRadio label="Accent" value={t.accent} options={["Blue", "Terracotta"]} onChange={(v) => setTweak("accent", v)} />
        <TweakRadio label="Band tint" value={t.band} options={["Soft", "Medium", "Strong"]} onChange={(v) => setTweak("band", v)} />
        <TweakRadio label="Heading serif" value={t.serif} options={["Garamond", "Caslon"]} onChange={(v) => setTweak("serif", v)} />
        <TweakSection label="Hero" />
        <TweakRadio label="Treatment" value={t.hero} options={["Centered", "Editorial"]} onChange={(v) => setTweak("hero", v)} />
        <TweakToggle label="Ken-Burns & autoplay" value={t.motion} onChange={(v) => setTweak("motion", v)} />
      </TweaksPanel>
    </React.Fragment>
  );
}

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