import { prisma } from "@/lib/prisma";
import TemplatesClient from "./TemplatesClient";

export const dynamic = "force-dynamic";

export default async function AdminOperationsPage() {
  const [templates, machines] = await Promise.all([
    prisma.operationTemplate.findMany({
      orderBy: [{ active: "desc" }, { name: "asc" }],
      include: { machine: { select: { id: true, name: true, active: true } } },
    }),
    prisma.machine.findMany({
      where: { active: true },
      orderBy: { name: "asc" },
      select: { id: true, name: true },
    }),
  ]);
  return (
    <TemplatesClient
      initialTemplates={templates.map((t) => ({
        id: t.id,
        name: t.name,
        machineId: t.machineId,
        machineName: t.machine?.name ?? null,
        defaultSettings: t.defaultSettings,
        defaultInstructions: t.defaultInstructions,
        qcRequired: t.qcRequired,
        active: t.active,
      }))}
      machines={machines}
    />
  );
}
