import {ReactNode} from "react";
import {notFound, redirect} from "next/navigation";
import {auth} from "@/auth";
import {prisma} from "@/lib/db/prisma";
import {AppShell} from "@/components/layout/app-shell";
import {CompanySidebar} from "@/components/dashboard/company-sidebar";

type CompanyLayoutProps = {
  children: ReactNode;
  params: Promise<{tenantSlug: string; locale: string}>;
};

export default async function CompanyLayout({children, params}: CompanyLayoutProps) {
  const {tenantSlug, locale} = await params;
  const session = await auth();

  if (!session?.user?.id) {
    redirect(`/${locale}/auth/sign-in`);
  }

  const tenant = await prisma.tenant.findUnique({
    where: {slug: tenantSlug}
  });

  if (!tenant) notFound();

  const sidebar = (
    <CompanySidebar
      tenantSlug={tenantSlug}
      companyName={tenant.name}
    />
  );

  return (
    <AppShell
      title={tenant.name}
      sidebar={sidebar}
    >
      {children}
    </AppShell>
  );
}
