"use server";

import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth-config";
import { redirect } from "next/navigation";

export async function deleteClass(formData: FormData) {
  const session = await getServerSession(authOptions);
  if (!session?.user) redirect("/login");
  if (!session.user.establishmentId && session.user.role !== "superadmin") redirect("/dashboard");

  const id = formData.get("id") as string;
  if (!id) return;

  const establishmentId = session.user.establishmentId;
  await prisma.class.deleteMany({
    where: { id, ...(establishmentId ? { establishmentId } : {}) },
  });

  redirect("/dashboard/classes");
}
