import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  console.log("🌱 Seeding Rombel data...");

  // Get existing jurusan
  const jurusanList = await prisma.jurusan.findMany();

  // Buat mapping jurusan berdasarkan kode atau nama
  const jurusanMap: Record<string, string> = {};
  for (const j of jurusanList) {
    jurusanMap[j.kodeJurusan] = j.id;
    jurusanMap[j.namaJurusan] = j.id;
  }

  // Data rombel untuk tahun 2024/2025
  const rombelData = [
    // Kelas X
    { namaRombel: "X RPL 1", tingkat: "X", jurusanKode: "RPL" },
    { namaRombel: "X RPL 2", tingkat: "X", jurusanKode: "RPL" },
    { namaRombel: "X TKJ 1", tingkat: "X", jurusanKode: "TKJ" },
    { namaRombel: "X TKJ 2", tingkat: "X", jurusanKode: "TKJ" },
    { namaRombel: "X AKL 1", tingkat: "X", jurusanKode: "AKL" },
    { namaRombel: "X AKL 2", tingkat: "X", jurusanKode: "AKL" },
    { namaRombel: "X OTKP 1", tingkat: "X", jurusanKode: "OTKP" },
    { namaRombel: "X BDP 1", tingkat: "X", jurusanKode: "BDP" },

    // Kelas XI
    { namaRombel: "XI RPL 1", tingkat: "XI", jurusanKode: "RPL" },
    { namaRombel: "XI RPL 2", tingkat: "XI", jurusanKode: "RPL" },
    { namaRombel: "XI TKJ 1", tingkat: "XI", jurusanKode: "TKJ" },
    { namaRombel: "XI TKJ 2", tingkat: "XI", jurusanKode: "TKJ" },
    { namaRombel: "XI AKL 1", tingkat: "XI", jurusanKode: "AKL" },
    { namaRombel: "XI AKL 2", tingkat: "XI", jurusanKode: "AKL" },
    { namaRombel: "XI OTKP 1", tingkat: "XI", jurusanKode: "OTKP" },
    { namaRombel: "XI BDP 1", tingkat: "XI", jurusanKode: "BDP" },

    // Kelas XII
    { namaRombel: "XII RPL 1", tingkat: "XII", jurusanKode: "RPL" },
    { namaRombel: "XII RPL 2", tingkat: "XII", jurusanKode: "RPL" },
    { namaRombel: "XII TKJ 1", tingkat: "XII", jurusanKode: "TKJ" },
    { namaRombel: "XII TKJ 2", tingkat: "XII", jurusanKode: "TKJ" },
    { namaRombel: "XII AKL 1", tingkat: "XII", jurusanKode: "AKL" },
    { namaRombel: "XII AKL 2", tingkat: "XII", jurusanKode: "AKL" },
    { namaRombel: "XII OTKP 1", tingkat: "XII", jurusanKode: "OTKP" },
    { namaRombel: "XII BDP 1", tingkat: "XII", jurusanKode: "BDP" },
  ];

  const tahunPelajaran = "2024/2025";
  let createdCount = 0;
  let skippedCount = 0;

  for (const rombel of rombelData) {
    // Check if rombel already exists
    const existing = await prisma.rombel.findUnique({
      where: {
        namaRombel_tahunPelajaran: {
          namaRombel: rombel.namaRombel,
          tahunPelajaran: tahunPelajaran,
        },
      },
    });

    if (existing) {
      console.log(`⏭️  Skipped: ${rombel.namaRombel} (already exists)`);
      skippedCount++;
      continue;
    }

    // Find jurusan ID
    const jurusanId = jurusanMap[rombel.jurusanKode] || null;

    if (!jurusanId && rombel.jurusanKode) {
      console.log(`⚠️  Warning: Jurusan ${rombel.jurusanKode} not found for ${rombel.namaRombel}`);
    }

    await prisma.rombel.create({
      data: {
        namaRombel: rombel.namaRombel,
        tingkat: rombel.tingkat,
        tahunPelajaran: tahunPelajaran,
        jurusanId: jurusanId,
        isActive: true,
      },
    });

    console.log(`✅ Created: ${rombel.namaRombel}`);
    createdCount++;
  }

  console.log(`\n📊 Summary:`);
  console.log(`   Created: ${createdCount}`);
  console.log(`   Skipped: ${skippedCount}`);
  console.log(`   Total: ${rombelData.length}`);
}

main()
  .then(async () => {
    await prisma.$disconnect();
    console.log("\n✨ Seeding completed!");
  })
  .catch(async (e) => {
    console.error("❌ Error during seeding:", e);
    await prisma.$disconnect();
    process.exit(1);
  });
