import path from "node:path";
import fs from "node:fs";
import { fileURLToPath } from "node:url";

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

const prisma = new PrismaClient();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const defaultSourcePath = path.join(__dirname, "data", "Grade.xls");
const sourcePath = process.argv[2] ?? process.env.GRADE_XLS_PATH ?? defaultSourcePath;

function formatCode(prefix, sequence) {
  return `${prefix}${String(sequence).padStart(5, "0")}`;
}

async function main() {
  if (!fs.existsSync(sourcePath)) {
    throw new Error(
      [
        `File grade tidak ditemukan: ${sourcePath}`,
        "Simpan file Grade.xls di scripts/data/Grade.xls",
        "atau jalankan: node scripts/seed-grades-from-xls.mjs /path/ke/Grade.xls",
        "atau set env: GRADE_XLS_PATH=/path/ke/Grade.xls"
      ].join("\n")
    );
  }

  const workbook = XLSX.readFile(sourcePath);
  const sheetName = workbook.SheetNames[0];
  const rows = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { defval: "" });

  let mangkokSequence = 1;
  let nonMangkokSequence = 1;

  for (const row of rows) {
    const legacyCode = String(row.Code || "").trim();
    const name = String(row.Name || "").trim();
    const description = String(row.Description || "").trim();
    const isMangkok = String(row.Mangkok || "").trim().toLowerCase() === "yes";

    if (!name) continue;

    const prefix = isMangkok ? "MGK" : "GRD";
    const code = isMangkok
      ? formatCode(prefix, mangkokSequence++)
      : formatCode(prefix, nonMangkokSequence++);

    await prisma.grade.upsert({
      where: { legacyCode: legacyCode || "__missing__" },
      update: {
        code,
        isMangkok,
        name,
        description: description || null,
        status: "ACTIVE"
      },
      create: {
        code,
        legacyCode: legacyCode || null,
        isMangkok,
        name,
        description: description || null,
        status: "ACTIVE"
      }
    });
  }

  const count = await prisma.grade.count();
  console.log(`Seeded grades: ${count} from ${path.basename(sourcePath)}`);
}

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