import { notFound } from 'next/navigation';
import { eq } from 'drizzle-orm';

import { TenancyDetail } from '@/components/tenancies/tenancy-detail';
import { db } from '@/lib/db';
import { ports as portsTable } from '@/lib/db/schema/ports';
import { isTenanciesModuleEnabled } from '@/lib/services/tenancies-module.service';

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

export default async function TenancyDetailPage({ params }: PageProps) {
  const { portSlug, id } = await params;
  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 <TenancyDetail tenancyId={id} portSlug={portSlug} />;
}
