import { redirect, notFound } from 'next/navigation'
import { getSession } from '@/lib/auth'
import { getAccessibleProduct } from '@/lib/product-access'
import { SetCurrentProduct } from '@/components/shared/set-current-product'

interface Props {
  children: React.ReactNode
  params: Promise<{ id: string }>
}

export default async function ProductLayout({ children, params }: Props) {
  const { id } = await params
  const session = await getSession()
  if (!session.userId) redirect('/login')

  const product = await getAccessibleProduct(id, session.userId)
  if (!product) notFound()

  return (
    <>
      <SetCurrentProduct id={id} name={product.name} />
      {children}
    </>
  )
}
