import { notFound } from "next/navigation";
import { prisma } from "@/lib/prisma";
import ProjectDetailClient from "./ProjectDetailClient";

export const dynamic = "force-dynamic";

export default async function AdminProjectDetailPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const project = await prisma.project.findUnique({
    where: { id },
    include: {
      assemblies: {
        orderBy: { code: "asc" },
        include: { _count: { select: { parts: true } } },
      },
      _count: { select: { fasteners: true, purchaseOrders: true } },
    },
  });
  if (!project) notFound();

  return (
    <ProjectDetailClient
      project={{
        id: project.id,
        code: project.code,
        name: project.name,
        customerCode: project.customerCode,
        status: project.status,
        dueDate: project.dueDate?.toISOString() ?? null,
        notes: project.notes,
        fastenerCount: project._count.fasteners,
        poCount: project._count.purchaseOrders,
      }}
      assemblies={project.assemblies.map((a) => ({
        id: a.id,
        code: a.code,
        name: a.name,
        qty: a.qty,
        notes: a.notes,
        partCount: a._count.parts,
      }))}
    />
  );
}
