import { db } from "@/app/db/db";
import { bulkAddressUploads } from "@/app/db/schema/bulk_address_uploads";
import { eq } from "drizzle-orm";
import { getServerSession } from "next-auth";
import { AuthOptions } from "@/app/api/auth/[...nextauth]/authOptions";
import { redirect, notFound } from "next/navigation";
import MapColumnsClient from "./MapColumnsClient";

export default async function MapColumnsPage(props: {
  params: Promise<{ slug: string; uploadId: string }>;
}) {
  const { slug, uploadId } = await props.params;
  const session = await getServerSession(AuthOptions);
  if (!session) redirect("/login");

  const [upload] = await db
    .select()
    .from(bulkAddressUploads)
    .where(eq(bulkAddressUploads.id, uploadId))
    .limit(1);

  if (!upload) notFound();

  return (
    <MapColumnsClient
      portfolioId={slug}
      uploadId={uploadId}
      filename={upload.filename}
      sourceHeaders={upload.sourceHeaders}
      existingMapping={upload.columnMapping ?? undefined}
    />
  );
}
