import { notFound, redirect } from 'next/navigation';
import { isAdmin } from '@/lib/admin';
import { Sidebar } from '@/components/sidebar';
import { DashboardHeader } from '@/components/dashboard-header';
import { CommandPalette } from '@/components/command-palette';
import type { Metadata } from 'next';
import { RegistryWebTelemetryIdentity } from '@/components/registry-web-telemetry';
import { WorkspaceStateProvider } from '@/components/workspace-state-provider';
import { getWorkspaceState, toClientWorkspaceState } from '@/lib/workspace-state';

export const dynamic = 'force-dynamic';

export const metadata: Metadata = {
  title: 'Admin',
};

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const workspace = await getWorkspaceState();

  if (!workspace) {
    redirect('/login');
  }
  if (!isAdmin(workspace.authUser.email ?? '')) {
    notFound();
  }
  const workspaceSnapshot = toClientWorkspaceState(workspace);

  return (
    <WorkspaceStateProvider value={workspaceSnapshot}>
      <RegistryWebTelemetryIdentity
        orgId={workspace.activeOrganization?.id ?? null}
        plan={workspace.tier}
        userId={workspace.authUser.id}
      />
      <div className="registry-shell-root">
        <a href="#main-content" className="registry-skip-link">
          Skip to content
        </a>
        <Sidebar workspace={workspaceSnapshot} />

        <div className="registry-shell-main">
          <DashboardHeader />
          <CommandPalette workspace={workspaceSnapshot} />
          <main id="main-content" className="registry-shell-body entrance-fade">
            {children}
          </main>
        </div>
      </div>
    </WorkspaceStateProvider>
  );
}
