// PBI-11 / ST-1134: Mobile shell-layout. Eigen route group (mobile) — nested
// layout in (app)/ kan parent NavBar/StatusBar/MinWidthBanner niet onderdrukken.
// Auth via gedeelde requireSession() (lib/auth-guard.ts), hergebruikt door
// (app)/layout.tsx.

import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'
import { requireSession } from '@/lib/auth-guard'
import { LandscapeGuard } from '@/components/mobile/landscape-guard'
import { MobileTabBar } from '@/components/mobile/mobile-tab-bar'

export default async function MobileLayout({ children }: { children: React.ReactNode }) {
  const session = await requireSession()

  // Active product nodig voor de tab-bar (Backlog/Solo-tabs verbergen als geen actief product).
  const user = await prisma.user.findUnique({
    where: { id: session.userId },
    select: { active_product_id: true },
  })

  let activeProductId: string | null = null
  if (user?.active_product_id) {
    const product = await prisma.product.findFirst({
      where: { id: user.active_product_id, archived: false, ...productAccessFilter(session.userId) },
      select: { id: true },
    })
    activeProductId = product?.id ?? null
  }

  return (
    <div className="h-screen bg-background flex flex-col overflow-hidden">
      <LandscapeGuard>
        <main id="main-content" className="flex-1 flex flex-col overflow-y-auto min-h-0 pb-14">
          {children}
        </main>
        <MobileTabBar activeProductId={activeProductId} />
      </LandscapeGuard>
    </div>
  )
}
