/* ============================================================
   Bizia — Root router & mount
   Maps hash routes to page components. Pages register on
   window.* and are looked up by name so the app degrades
   gracefully while screens are still being built.
   ============================================================ */
(function () {
  const { useState, useEffect } = React;

  // route prefix -> { page: windowGlobalName, shell, fullBleed, prop }
  const ROUTES = [
    { match: '/', exact: true, page: 'LandingPage', shell: false },
    { match: '/login', page: 'AuthPage', shell: false, prop: { mode: 'login' } },
    { match: '/signup', page: 'AuthPage', shell: false, prop: { mode: 'signup' } },
    { match: '/forgot-password', page: 'AuthPage', shell: false, prop: { mode: 'forgot' } },
    { match: '/invite', page: 'AuthPage', shell: false, prop: { mode: 'invite' } },
    { match: '/app/new', page: 'WizardPage', shell: false },
    { match: '/app/builder', page: 'BuilderPage', shell: true, fullBleed: true },
    { match: '/app/recommendation', page: 'RecommendationPage', shell: true },
    { match: '/app/templates', page: 'TemplatesPage', shell: true },
    { match: '/app/pages', page: 'PagesPage', shell: true },
    { match: '/app/design', page: 'DesignPage', shell: true },
    { match: '/app/domain', page: 'DomainPage', shell: true },
    { match: '/app/infrastructure', page: 'HostingPage', shell: true },
    { match: '/app/seo', page: 'SeoPage', shell: true },
    { match: '/app/forms', page: 'FormsPage', shell: true },
    { match: '/app/integrations', page: 'IntegrationsPage', shell: true },
    { match: '/app/social-kit', page: 'SocialPage', shell: true },
    { match: '/app/ecards', page: 'EcardsPage', shell: true },
    { match: '/app/leads', page: 'LeadsPage', shell: true },
    { match: '/app/analytics', page: 'AnalyticsPage', shell: true },
    { match: '/app/billing', page: 'BillingPage', shell: true },
    { match: '/app/settings', page: 'SettingsPage', shell: true },
    { match: '/app/free-stack', page: 'FreeStackPage', shell: true },
    { match: '/app/account-hub', page: 'AccountHubPage', shell: true },
    { match: '/app/new-website', page: 'NewWebsitePage', shell: false },
    { match: '/app/payment', page: 'PaymentPage', shell: false },
    { match: '/app/go-live', page: 'GoLivePage', shell: true },
    { match: '/app/admin/client-readiness', page: 'AdminClientReadinessPage', shell: true },
    { match: '/app/preview', page: 'PreviewPage', shell: false },
    { match: '/client/review', page: 'ClientReviewPage', shell: false },
    { match: '/app', page: 'DashboardPage', shell: true },
  ];

  function resolve(path) {
    const base = path.split('?')[0];
    for (const r of ROUTES) {
      if (r.exact ? base === r.match : (base === r.match || base.startsWith(r.match + '/') || base === r.match)) {
        return r;
      }
    }
    // startsWith for /app etc handled; fallback
    return ROUTES.find(r => !r.exact && base.startsWith(r.match)) || ROUTES[0];
  }

  function ComingSoon({ name }) {
    const label = (name || '').replace('Page', '');
    return React.createElement('div', null,
      React.createElement(window.PageHeader, { title: label || 'Screen', sub: 'This screen is part of the build and will appear here shortly.' }),
      React.createElement(window.EmptyState, {
        icon: 'layers', title: 'Under construction',
        desc: 'This route is wired up — the full screen is being built in the current milestone.',
        action: React.createElement(window.Button, { variant: 'outline', icon: 'arrowLeft', onClick: () => window.navigate('/app') }, 'Back to overview'),
      }),
    );
  }

  function Root() {
    const path = window.useRoute();
    const route = resolve(path);
    const Page = window[route.page];
    const content = Page
      ? React.createElement(Page, { ...(route.prop || {}), path })
      : React.createElement(ComingSoon, { name: route.page });

    if (route.shell) {
      return React.createElement(window.AppShell, { fullBleed: route.fullBleed }, content);
    }
    return content;
  }

  function App() {
    return React.createElement(React.Fragment, null,
      React.createElement(Root, null),
      React.createElement(window.ToastHost, null),
    );
  }

  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(React.createElement(App));
  window.__BIZIA_READY = true;
})();
