/* ============================================================
   EOLODENT — App router & mount
   ============================================================ */

function AdminApp() {
  const store = useStore();
  const hash = useHash();
  const [branchFilter, setBranchFilter] = useState("all");
  const session = store.session();
  if (!session) return <AdminLogin />;

  // hash like #/admin or #/admin/citas?x
  const rest = hash.replace(/^#\/admin\/?/, "").split("?")[0];
  const sub = rest || "";
  let view;
  if (sub === "" ) view = <AdminDashboard branchFilter={branchFilter} />;
  else if (sub === "citas") view = <AdminCitas branchFilter={branchFilter} />;
  else if (sub === "pacientes") view = <AdminPacientes branchFilter={branchFilter} />;
  else if (sub === "servicios") view = <AdminServicios />;
  else if (sub === "blog") view = <AdminBlog />;
  else if (sub === "sucursales") view = <AdminSucursales />;
  else view = <AdminDashboard branchFilter={branchFilter} />;

  return <AdminShell active={sub} branchFilter={branchFilter} setBranchFilter={setBranchFilter}>{view}</AdminShell>;
}

function App() {
  const hash = useHash();
  const path = hash.replace(/^#/, "").split("?")[0];

  // Admin routes render full-screen (no public header/footer)
  if (path.startsWith("/admin")) return <AdminApp />;

  let page;
  if (path === "/" || path === "" || path === "/inicio") page = <HomePage />;
  else if (path === "/servicios") page = <ServiciosPage />;
  else if (path === "/sucursales") page = <SucursalesPage />;
  else if (path === "/blog") page = <BlogPage />;
  else if (path.startsWith("/blog/")) page = <BlogPostPage slug={path.replace("/blog/", "")} />;
  else if (path === "/agenda") page = <AgendaPage />;
  else page = <HomePage />;

  return (
    <>
      <Header />
      {page}
      <Footer />
    </>
  );
}

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