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

export async function ensureSystemUser() {
  const existingUser = await prisma.user.findFirst({
    where: { email: "system@abelbirdnest.local" }
  });

  if (existingUser) {
    return existingUser;
  }

  const role = await prisma.role.upsert({
    where: { code: "SYSTEM_ADMIN" },
    update: {},
    create: {
      code: "SYSTEM_ADMIN",
      name: "System Admin"
    }
  });

  return prisma.user.create({
    data: {
      roleId: role.id,
      name: "System Admin",
      email: "system@abelbirdnest.local",
      status: "ACTIVE"
    }
  });
}
