import { notFound, redirect } from "next/navigation";
import { db } from "@/lib/db";
import { auth } from "@/auth";
import { EdlImportClient } from "./EdlImportClient";

export async function generateMetadata({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const project = await db.project.findUnique({ where: { id }, select: { name: true } });
  return { title: `Import VFX Pull — ${project?.name ?? "Project"}` };
}

export default async function ImportEdlPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const session = await auth();
  if (!session?.user) redirect("/login");
  if (!["ADMIN", "PRODUCER", "SUPERVISOR"].includes(session.user.role)) {
    redirect(`/projects/${id}`);
  }

  const project = await db.project.findUnique({
    where: { id },
    select: {
      id: true,
      name: true,
      shots: { select: { shotCode: true } },
    },
  });
  if (!project) notFound();

  const existingShotCodes = new Set(project.shots.map((s) => s.shotCode));

  return (
    <EdlImportClient
      projectId={project.id}
      projectName={project.name}
      existingShotCodes={existingShotCodes}
    />
  );
}
