import { notFound, redirect } from "next/navigation";

import { auth } from "@/auth";
import { prisma } from "@/lib/db";
import { projectAccessFilter } from "@/lib/access";
import { getBoard } from "@/lib/board";
import { getAiSettingsView } from "@/lib/ai-settings";
import { KanbanBoard } from "@/components/board/kanban-board";

export default async function ProjectPage({
  params,
}: {
  params: Promise<{ projectId: string }>;
}) {
  const session = await auth();
  if (!session?.user) redirect("/login");
  const { projectId } = await params;

  const project = await prisma.project.findFirst({
    where: { id: projectId, ...projectAccessFilter(session.user.id) },
    select: { id: true, title: true },
  });
  // Same response whether the project doesn't exist or just isn't this
  // user's -- no need to distinguish "not found" from "not yours".
  if (!project) notFound();

  const [board, aiSettings] = await Promise.all([
    getBoard({ projectId: project.id }),
    getAiSettingsView(),
  ]);
  const aiConfigured = !!(aiSettings.apiUrl && aiSettings.model);

  return (
    <KanbanBoard
      initialCategories={board}
      projectId={project.id}
      title={project.title}
      aiConfigured={aiConfigured}
    />
  );
}
