/* global React, ReactDOM, useHashRoute,
   Nav, Footer,
   HomePage, MarketPage, TrustPage, AgentsPage, WhitepaperPage, AboutPage,
   RoadmapPage, DevelopersPage, PartnersPage, InvestorsPage,
   PressPage, GlossaryPage, GlossaryEntryPage,
   VsPayPerCheckPage, VsClosedPage,
   LegalIndexPage, LegalSubPage, NotFoundPage,
   AssetsChecklistPage */

const { useEffect } = React;

const TITLES = {
  '/': ['BitID — The identity layer for a machine-first world', 'Zero-knowledge identity for humans, apps, and machines. Anchored on Bitcoin.'],
  '/market': ['Market — A $265B surface · BitID', 'The pay-per-check stack is obsolete. One primitive replaces the bundle.'],
  '/trust': ['Trust — Bank-grade. Zero-knowledge by default · BitID', 'Compliance, ZK, and Bitcoin anchoring.'],
  '/agents': ['Agents — The machine-agent thesis · BitID', 'The first identity primitive designed for machines acting on behalf of verified humans.'],
  '/whitepaper': ['Whitepaper v0.9 · BitID', 'The BitID Whitepaper — preprint, external edition.'],
  '/about': ['About · BitID', 'Built by the team that invented identity tokenization.'],
  '/roadmap': ['Roadmap · BitID', 'Public, dated commitments.'],
  '/developers': ['Developer early access · BitID', 'When the SDK ships, you get it first.'],
  '/partners': ['Partner access · BitID', 'Reference integrations launch with mainnet in Q4 2026.'],
  '/investors': ['Investor brief · BitID', 'Infrastructure-tier funds.'],
  '/press': ['Press kit · BitID', 'One-liner, summary, palette, assets.'],
  '/glossary': ['Glossary · BitID', 'The vocabulary of a machine-first identity layer.'],
  '/vs/pay-per-check-identity': ['BitID vs pay-per-check identity', 'One always-on credential vs per-call charges.'],
  '/vs/closed-identity': ['BitID vs closed identity', 'Neutral and portable vs platform-owned.'],
  '/legal': ['Legal · BitID', 'Terms, privacy, acceptable use.'],
  '/legal/terms': ['Terms of Service · BitID', 'Effective April 2026.'],
  '/legal/privacy': ['Privacy · BitID', 'What we collect, what we don\'t.'],
  '/legal/acceptable-use': ['Acceptable Use · BitID', 'What BitID may and may not be used for.'],
  '/assets-checklist': ['Card asset checklist · BitID', 'Pipeline status for the Beem Mastercard + BitID card system.'],
};

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

  useEffect(() => {
    const title = TITLES[route] ? TITLES[route][0] : (TITLES[route.replace(/\/[^/]+$/, '/')] || TITLES['/'])[0];
    const desc = TITLES[route] ? TITLES[route][1] : TITLES['/'][1];
    document.title = title;
    let descTag = document.querySelector('meta[name="description"]');
    if (descTag) descTag.setAttribute('content', desc);
    // /whitepaper now uses the dark system; no theme switch needed.
    document.body.classList.remove('theme-light');
  }, [route]);

  // Routing
  if (route === '/') return <Shell><HomePage /></Shell>;
  if (route === '/market') return <Shell><MarketPage /></Shell>;
  if (route === '/trust') return <Shell><TrustPage /></Shell>;
  if (route === '/agents') return <Shell><AgentsPage /></Shell>;
  if (route === '/whitepaper') return <Shell><WhitepaperPage /></Shell>;
  if (route === '/about') return <Shell><AboutPage /></Shell>;
  if (route === '/roadmap') return <Shell><RoadmapPage /></Shell>;
  if (route === '/developers') return <Shell><DevelopersPage /></Shell>;
  if (route === '/partners') return <Shell><PartnersPage /></Shell>;
  if (route === '/investors') return <Shell><InvestorsPage /></Shell>;
  if (route === '/press') return <Shell><PressPage /></Shell>;
  if (route === '/glossary') return <Shell><GlossaryPage /></Shell>;
  if (route.startsWith('/glossary/')) return <Shell><GlossaryEntryPage slug={route.split('/')[2]} /></Shell>;
  if (route === '/vs/pay-per-check-identity') return <Shell><VsPayPerCheckPage /></Shell>;
  if (route === '/vs/closed-identity') return <Shell><VsClosedPage /></Shell>;
  if (route === '/legal') return <Shell><LegalIndexPage /></Shell>;
  if (route === '/legal/terms') return <Shell><LegalSubPage slug="terms" title="Terms of Service" /></Shell>;
  if (route === '/legal/privacy') return <Shell><LegalSubPage slug="privacy" title="Privacy" /></Shell>;
  if (route === '/legal/acceptable-use') return <Shell><LegalSubPage slug="acceptable-use" title="Acceptable Use" /></Shell>;
  if (route === '/assets-checklist') return <Shell><AssetsChecklistPage /></Shell>;
  return <Shell route={route}><NotFoundPage /></Shell>;
}

function Shell({ children }) {
  const route = useHashRoute();
  return (
    <>
      <Nav route={route} />
      {children}
      <Footer />
    </>
  );
}

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