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

import { ScanShell } from '@/components/scan/scan-shell';
import { db } from '@/lib/db';
import { ports } from '@/lib/db/schema/ports';
import { getPortBrandingConfig } from '@/lib/services/port-config';
import { getResolvedOcrConfig } from '@/lib/services/ocr-config.service';

export const metadata: Metadata = {
  title: 'Scan receipt',
};

export default async function ScanPage({ params }: { params: Promise<{ portSlug: string }> }) {
  const { portSlug } = await params;
  const port = await db.query.ports.findFirst({ where: eq(ports.slug, portSlug) });
  const branding = port ? await getPortBrandingConfig(port.id).catch(() => null) : null;
  // CM-6: manual-entry mode is resolved server-side so the client can skip
  // on-device parsing entirely (no wasted Tesseract pass) and open an empty form.
  const ocr = port ? await getResolvedOcrConfig(port.id).catch(() => null) : null;
  return (
    <ScanShell
      logoUrl={branding?.logoUrl ?? null}
      portName={port?.name ?? null}
      manualEntry={ocr?.manualEntry ?? false}
    />
  );
}
