import { PrismaClient } from "../generated/client.ts";

const prisma = new PrismaClient();

const orders = [
  { id:  -1, name: "Anna",    pizza: "Paesana" },
  { id:  -2, name: "Susi",    pizza: "Broccoli" },
  { id:  -3, name: "Fritz",   pizza: "Rucola" },
  { id:  -4, name: "Andrea",  pizza: "Broccoli" },
  { id:  -5, name: "Thomas",  pizza: "Paesana" },
  { id:  -6, name: "Verena",  pizza: "Rucola" },
  { id:  -7, name: "Marion",  pizza: "Broccoli" },
  { id:  -8, name: "Karl",    pizza: "Capricciosa" },
  { id:  -9, name: "Hans",    pizza: "Diavola" },
  { id: -10, name: "Barbara", pizza: "Capricciosa" }
];

async function main() {
  console.log("Remove old testdata...");

  const result = await prisma.order.deleteMany({
    where: {
      id: { lt: 0 },
    },
  });
  console.log(`Deleted ${result.count} orders with negative id`);

  console.log("Start seeding...");

  for (const order of orders) {
    await prisma.order.create({
      data: order,
    });
    console.log(`Created order with id: ${order.id}`);
  }

  console.log("Seeding finished.");
}

try {
    await main();
}
catch (e) {
    console.error(e);
    Deno.exit(1);
}
finally {
    await prisma.$disconnect();
}