import Image from 'next/image';
import '../globals.css';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { redirect } from 'next/navigation';
import { getServerSession } from 'next-auth';
import prisma from '@/lib/prisma';
import WorkspaceNavigation from './WorkspaceNavigation';

export default async function DashboardLayout({ children }: Readonly<{ children: React.ReactNode }>) {
	const session = await getServerSession();

	if (!session) {
		redirect('/sign-in');
	}

	const workspaces = await prisma.workspace.findMany({
		where: {
			ownerId: session.user.id as string,
		},
	});

	return (
		<>
			<div className="bg-white flex justify-center pt-4">
				<div className="container">
					<div className="flex flex-row justify-between items-center">
						<div>
							<Image src="/stalin-deploy.png" alt="AkeTechnology" width={200} height={35} />
						</div>
						<div className="flex gap-2">
							<WorkspaceNavigation workspaces={workspaces} />
							<Avatar>
								<AvatarImage src={session.user.image as string} alt={session.user.name as string} />
								<AvatarFallback>{session.user.name.at(0) as string}</AvatarFallback>
							</Avatar>
						</div>
					</div>
				</div>
			</div>
			{children}
		</>
	);
}
