// CENTRAL DA MARCA — Sanvi Activewear · App: gate de login + roteamento.
// Exporta window.SV_App
(function () {
  const { useState, useEffect } = React;
  const h = React.createElement;
  const S = window.SV_STORE;
  const { Login, Sidebar } = window.SV_UI;

  const ROUTES = ["inicio", "aprovar-ideias", "aprovar-design", "posts", "campanha", "calendario", "divulgacao", "arquivos", "ferramentas"];
  function initRota() { const hh = decodeURIComponent((location.hash || "").replace(/^#\/?/, "")); return ROUTES.includes(hh) ? hh : "inicio"; }

  function App() {
    const [authed, setAuthed] = useState(S.isAuthed());
    const [rota, setRota] = useState(initRota);
    const [open, setOpen] = useState(false);

    useEffect(() => {
      function onHash() { const hh = decodeURIComponent((location.hash || "").replace(/^#\/?/, "")); if (ROUTES.includes(hh)) setRota(hh); }
      window.addEventListener("hashchange", onHash);
      return () => window.removeEventListener("hashchange", onHash);
    }, []);

    function go(r) { setRota(r); if (history.replaceState) history.replaceState(null, "", "#" + r); }
    function logout() { S.logout(); setAuthed(false); }

    if (!authed) return h(Login, { onOk: () => setAuthed(true) });

    const PAGES = {
      "inicio": () => h(window.SV_Inicio, { go }),
      "aprovar-ideias": window.SV_Ideias,
      "aprovar-design": window.SV_Design,
      "posts": window.SV_Posts,
      "campanha": window.SV_Campanha,
      "calendario": window.SV_Calendario,
      "divulgacao": () => h(window.SV_Divulgacao, { go }),
      "arquivos": window.SV_Arquivos,
      "ferramentas": window.SV_Ferramentas,
    };
    const Page = PAGES[rota] || PAGES["inicio"];
    return h("div", { className: "shell" },
      h("div", { className: "topbar" },
        h("div", { className: "topbar-mark" }, "S"),
        h("div", { style: { fontFamily: "'Syne',sans-serif", fontWeight: 600, fontSize: 20 } }, "Sanvi Activewear"),
        h("button", { className: "burger", onClick: () => setOpen((o) => !o) }, "☰")
      ),
      h(Sidebar, { rota, go, open, setOpen, onLogout: logout }),
      h("main", null, h(Page))
    );
  }

  window.SV_App = App;
})();
