import type { Metadata } from 'next';
import { eq } from 'drizzle-orm';

import { UploadReceiptsGuide } from '@/components/invoices/upload-receipts-guide';
import { db } from '@/lib/db';
import { ports as portsTable } from '@/lib/db/schema/ports';
import { isExpensesModuleEnabled } from '@/lib/services/expenses-module.service';
import { ModuleDisabledPage } from '@/components/shared/module-disabled-page';

export const metadata: Metadata = {
  title: 'How to upload receipts',
};

export default async function UploadReceiptsPage({
  params,
}: {
  params: Promise<{ portSlug: string }>;
}) {
  const { portSlug } = await params;
  // Mirrors the /expenses layout gate: the receipt-upload explainer is
  // part of the expenses surface, so the same port-scoped toggle
  // blocks it. /invoices/upload-receipts isn't under /expenses, so a
  // separate gate is needed here.
  const port = await db.query.ports.findFirst({
    where: eq(portsTable.slug, portSlug),
    columns: { id: true },
  });
  if (port && !(await isExpensesModuleEnabled(port.id))) {
    return (
      <ModuleDisabledPage
        moduleName="Expenses"
        description="Receipt upload is part of the Expenses module, which is turned off for this port."
        settingsHref={`/${portSlug}/admin/settings`}
        fallbackHref={`/${portSlug}/dashboard`}
      />
    );
  }
  return <UploadReceiptsGuide portSlug={portSlug} />;
}
