// Standalone admin page bootstrap.
// In production this is a SEPARATE Cloudflare Pages deployment, gated by
// Cloudflare Zero Trust (Access). The moderator's identity comes from the
// Access JWT; here it's mocked via the shared persisted identity.

// Inherit theme/density from the main site (persisted under archive_tweaks).
(function applyTheme() {
  try {
    const t = JSON.parse(localStorage.getItem('archive_tweaks') || '{}');
    document.documentElement.dataset.theme = t.dark ? 'dark' : 'light';
    document.documentElement.dataset.density = t.density === 'compact' ? 'compact' : 'comfortable';
  } catch (e) {
    document.documentElement.dataset.theme = 'light';
    document.documentElement.dataset.density = 'comfortable';
  }
})();

function AdminPage() {
  const identity = React.useMemo(() => window.loadIdentity(), []);
  const [toasts, setToasts] = React.useState([]);

  React.useEffect(() => {
    window.__pushToast = (t) => {
      const id = Date.now() + Math.random();
      setToasts((prev) => [...prev, { id, ...t }]);
      setTimeout(() => setToasts((prev) => prev.filter((x) => x.id !== id)), 3600);
    };
    return () => { window.__pushToast = null; };
  }, []);

  // "Back to site" leaves the protected admin deployment for the public site.
  const exit = () => { window.location.href = window.SITE_URL || 'index.html'; };

  return (
    <React.Fragment>
      <window.AdminView identity={identity} onExit={exit} />
      <window.Toasts items={toasts} />
    </React.Fragment>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<AdminPage />);
