const { PrismaClient } = require("@prisma/client");
const bcrypt = require("bcryptjs");

const prisma = new PrismaClient();

async function main() {
  const email = "admin@k-creation.com";
  const password = "AaaBbbCcc123";
  const hashedPassword = await bcrypt.hash(password, 10);

  const existingAdmin = await prisma.user.findUnique({
    where: { email },
  });

  if (!existingAdmin) {
    await prisma.user.create({
      data: {
        email,
        password: hashedPassword,
        name: "Admin",
        role: "admin",
      },
    });
    console.log("Admin user created successfully!");
  } else {
    // Update password if it already exists to ensure it matches
    await prisma.user.update({
      where: { email },
      data: { password: hashedPassword, role: "admin" },
    });
    console.log("Admin user updated successfully!");
  }
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
