// CENTRAL DA MARCA — Mariana Cabral · App: gate de login + roteamento.
// Ambiente 100% separado — não referencia nem dá acesso a nenhum outro sistema.
// Exporta window.MC_App
(function () {
  const { useState, useEffect } = React;
  const h = React.createElement;
  const S = window.MC_STORE;
  const { Login, Sidebar } = window.MC_UI;

  const ROUTES = ["inicio", "aprovar-ideias", "aprovar-design", "posts", "campanha", "calendario", "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.MC_Inicio, { go }),
      "aprovar-ideias": window.MC_Ideias,
      "aprovar-design": window.MC_Design,
      "posts": window.MC_Posts,
      "campanha": window.MC_Campanha,
      "calendario": window.MC_Calendario,
      "arquivos": window.MC_Arquivos,
      "ferramentas": window.MC_Ferramentas,
    };
    const Page = PAGES[rota] || PAGES["inicio"];
    return h("div", { className: "shell" },
      h("div", { className: "topbar" },
        h("div", { className: "topbar-mark" }, "M"),
        h("div", { style: { fontFamily: "'Syne',sans-serif", fontWeight: 600, fontSize: 20 } }, "Mariana Cabral"),
        h("button", { className: "burger", onClick: () => setOpen((o) => !o) }, "☰")
      ),
      h(Sidebar, { rota, go, open, setOpen, onLogout: logout }),
      h("main", null,
        rota !== "inicio" && h("button", { className: "backbar", onClick: () => go("inicio") }, h("span", { className: "backbar-ic" }, "‹"), "Voltar ao início"),
        h(Page)
      )
    );
  }

  window.MC_App = App;
})();
