// Server component layout that resolves the current user once and
// hands them to the client sidebar/topbar. Middleware already ensures
// a user is present, but we keep a guard here for type narrowing.

import { redirect } from 'next/navigation';
import { getCurrentUser } from '@/lib/auth/session';
import { Sidebar } from '@/components/features/Sidebar';
import { Topbar } from '@/components/features/Topbar';

export default async function MainLayout({ children }: { children: React.ReactNode }) {
  const user = await getCurrentUser();
  if (!user) redirect('/login');

  return (
    <div className="flex h-screen bg-gray-50">
      <Sidebar role={user.role} />
      <div className="flex flex-1 flex-col overflow-hidden">
        <Topbar user={{ name: user.name, role: user.role }} />
        <main className="flex-1 overflow-y-auto px-6 py-6">{children}</main>
      </div>
    </div>
  );
}
