import { notFound } from 'next/navigation';

import { TenanciesListPage } from '@/components/tenancies/tenancies-list-page';
import { db } from '@/lib/db';
import { ports as portsTable } from '@/lib/db/schema/ports';
import { eq } from 'drizzle-orm';
import { isTenanciesModuleEnabled } from '@/lib/services/tenancies-module.service';

interface PageProps {
  params: Promise<{ portSlug: string }>;
}

export default async function BerthTenanciesPage({ params }: PageProps) {
  const { portSlug } = await params;
  // Per docs/tenancies-design.md §"When disabled": top-level page returns
  // 404 when the module is off. The sidebar entry is already hidden via
  // tenanciesModuleByPort, so this 404 guards against direct URL access.
  const port = await db.query.ports.findFirst({
    where: eq(portsTable.slug, portSlug),
    columns: { id: true },
  });
  if (!port) notFound();
  if (!(await isTenanciesModuleEnabled(port.id))) notFound();

  return <TenanciesListPage />;
}
