/* Torre Auro — App router and bootstrap */

function App() {
  const route = useRoute();

  // Resolve which page to render
  let page;
  let key = route;
  if (route === '/' || route === '') {
    page = <Home />;
    key = 'home';
  } else if (route === '/contacto') {
    page = <ContactPage />;
  } else if (route === '/perspectiva' || route === '/perspectiva/') {
    page = <PerspectivaIndex />;
    key = 'perspectiva';
  } else if (route.startsWith('/perspectiva/')) {
    const slug = route.replace(/^\/perspectiva\//, '').replace(/\/$/, '');
    const post = POST_BY_SLUG[slug];
    if (post) {
      page = <PerspectivaPost post={post} />;
      key = 'post-' + slug;
    } else {
      page = <PerspectivaIndex />;
      key = 'perspectiva';
      window.history.replaceState({}, '', '/perspectiva');
    }
  } else if (route === '/eventos' || route === '/eventos/') {
    page = <EventosIndex />;
    key = 'eventos';
  } else if (route.startsWith('/eventos/')) {
    const slug = route.replace(/^\/eventos\//, '').replace(/\/$/, '');
    const ev = EVENT_BY_SLUG[slug];
    if (ev) {
      page = <EventoDetail event={ev} />;
      key = 'event-' + slug;
    } else {
      page = <EventosIndex />;
      key = 'eventos';
      window.history.replaceState({}, '', '/eventos');
    }
  } else {
    // try match a floor by its route ID
    const slug = route.replace(/^\/+/, '');
    const floor = FLOOR_BY_ID[slug];
    if (floor) {
      page = <LevelPage floor={floor} />;
    } else {
      // 404 — redirect to home
      page = <Home />;
      if (typeof window !== 'undefined') {
        window.history.replaceState({}, '', '/');
      }
      key = 'home';
    }
  }

  // Update document title
  useEffect(() => {
    const t = (() => {
      if (route === '/') return 'Torre Auro · El ecosistema empresarial de Boca del Río';
      if (route === '/contacto') return 'Contacto · Torre Auro';
      if (route === '/perspectiva') return 'Perspectiva · Torre Auro';
      if (route.startsWith('/perspectiva/')) {
        const s = route.replace(/^\/perspectiva\//, '').replace(/\/$/, '');
        const p = POST_BY_SLUG[s];
        return p ? `${p.title} · Perspectiva` : 'Perspectiva · Torre Auro';
      }
      if (route === '/eventos') return 'El Calendario · Torre Auro';
      if (route.startsWith('/eventos/')) {
        const s = route.replace(/^\/eventos\//, '').replace(/\/$/, '');
        const ev = EVENT_BY_SLUG[s];
        return ev ? `${ev.title} · Torre Auro` : 'El Calendario · Torre Auro';
      }
      const slug = route.replace(/^\/+/, '');
      const f = FLOOR_BY_ID[slug];
      if (f) return `${f.sh} · ${f.short} · Torre Auro`;
      return 'Torre Auro';
    })();
    document.title = t;
  }, [route]);

  return (
    <div className="app-root">
      <Nav route={route} />
      <main key={key}>
        {page}
      </main>
      <Footer />
      <ChatWidget />
    </div>
  );
}

const root = ReactDOM.createRoot(document.getElementById('app'));
root.render(<App />);
