import { redirect } from "next/navigation";

import '@/styles/globals.css'

import Navbar from "@/components/store-navbar";
import prismadb from "@/lib/prisma";

import { getServerSession } from "next-auth";
import BackgroundImage from "@/components/background-image";

export default async function DashboardLayout({
  children,
  params
}: {
  children: React.ReactNode;
  params: { storeId: string }
}) {
  const session = await getServerSession();
  const userId = session?.user?.email;

  if (!userId) {
    redirect('/login');
  }

  const store = await prismadb.store.findFirst({
    where: {
      id: params.storeId,
    },
    include: {
      userToStore: {
        where: {
          userId: userId
        }
      }
    }
  });

  if (!store) {
    redirect('/stores')
  }

  return (
    <>
      <Navbar />
      <BackgroundImage />
      {children}
    </>
  );
};